blob: 1eba780647dfe4eb470a84eadf7031d65c64356f [file] [log] [blame]
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package appengine contains an AppEngine app for
package appengine
import (
func mustGetenv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Panicf("%s environment variable not set.", k)
return v
// appHandler is the default handler, registered to serve "/".
// It creates a new App instance using the appengine Context and then
// dispatches the request to the App. The environment variable
// STORAGE_URL_BASE must be set in app.yaml with the name of the bucket to
// write to.
func appHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
// urlfetch defaults to 5s timeout if the context has no timeout.
// The underlying request has a 60 second timeout, so we might as well propagate that here.
// (Why doesn't appengine do that for us?)
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
app := &app.App{
StorageClient: &storage.Client{
BaseURL: mustGetenv("STORAGE_URL_BASE"),
HTTPClient: urlfetch.Client(ctx),
mux := http.NewServeMux()
mux.ServeHTTP(w, r)
func init() {
http.HandleFunc("/", appHandler)