github.com/jgbaldwinbrown/perf@v0.1.1/analysis/appengine/app.go (about)

     1  // Copyright 2017 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This binary contains an App Engine app for perf.golang.org
     6  package main
     7  
     8  import (
     9  	"log"
    10  	"net/http"
    11  	"os"
    12  	"time"
    13  
    14  	"golang.org/x/net/context"
    15  	"golang.org/x/perf/analysis/app"
    16  	"golang.org/x/perf/storage"
    17  	"google.golang.org/appengine"
    18  )
    19  
    20  func mustGetenv(k string) string {
    21  	v := os.Getenv(k)
    22  	if v == "" {
    23  		log.Panicf("%s environment variable not set.", k)
    24  	}
    25  	return v
    26  }
    27  
    28  // appHandler is the default handler, registered to serve "/".
    29  // It creates a new App instance using the appengine Context and then
    30  // dispatches the request to the App. The environment variable
    31  // STORAGE_URL_BASE must be set in app.yaml with the name of the bucket to
    32  // write to.
    33  func appHandler(w http.ResponseWriter, r *http.Request) {
    34  	ctx := appengine.NewContext(r)
    35  	// urlfetch defaults to 5s timeout if the context has no timeout.
    36  	// The underlying request has a 60 second timeout, so we might as well propagate that here.
    37  	// (Why doesn't appengine do that for us?)
    38  	ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
    39  	defer cancel()
    40  	app := &app.App{
    41  		BaseDir: "analysis/appengine", // relative to module root
    42  		StorageClient: &storage.Client{
    43  			BaseURL:    mustGetenv("STORAGE_URL_BASE"),
    44  			HTTPClient: http.DefaultClient,
    45  		},
    46  	}
    47  	mux := http.NewServeMux()
    48  	app.RegisterOnMux(mux)
    49  	mux.ServeHTTP(w, r)
    50  }
    51  
    52  func main() {
    53  	http.HandleFunc("/", appHandler)
    54  	appengine.Main()
    55  }