diff options
| author | Dmitri Shuralyov <dmitshur@golang.org> | 2019-03-05 18:22:19 -0500 |
|---|---|---|
| committer | Dmitri Shuralyov <dmitshur@golang.org> | 2019-03-07 18:39:40 +0000 |
| commit | 45a20b0d69ef2b06d2edfd97210a0df9ffd7f829 (patch) | |
| tree | 1746bfdf2f2701f353f7f92eeb147b6e4a15de8c /local.go | |
| parent | 838ce51196a7e9f9bff83402caf4f678dc0f0d75 (diff) | |
| download | golang-id-tour-45a20b0d69ef2b06d2edfd97210a0df9ffd7f829.tar.xz | |
tour: deploy with App Engine Standard on Go 1.11
This change upgrades the deployment of tour to use the newer
Go 1.11 runtime. As part of that, the appengine build tag is
removed (it's no longer set by App Engine), and the GAE_ENV
environment variable is used to detect when tour is being run
in App Engine mode.
Set an environment variable in app.yaml to configure the
x/tools/godoc/golangorgenv package appropriately.
Factor out the static file handlers in local.go, but keep
static file handlers in app.yaml for improved latency across
global regions.
Updates golang/go#30486
Change-Id: Ia5bc88aab34fd07bf6ff0785da831180f509156f
Reviewed-on: https://go-review.googlesource.com/c/tour/+/165537
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'local.go')
| -rw-r--r-- | local.go | 34 |
1 files changed, 22 insertions, 12 deletions
@@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build !appengine - package main import ( @@ -31,7 +29,7 @@ import ( ) const ( - basePkg = "golang.org/x/tour/" + basePkg = "golang.org/x/tour" socketPath = "/socket" ) @@ -76,6 +74,12 @@ func findRoot() (string, error) { func main() { flag.Parse() + if os.Getenv("GAE_ENV") == "standard" { + log.Println("running in App Engine Standard mode") + gaeMain() + return + } + // find and serve the go tour files root, err := findRoot() if err != nil { @@ -106,12 +110,7 @@ func main() { origin := &url.URL{Scheme: "http", Host: host + ":" + port} http.Handle(socketPath, socket.NewHandler(origin)) - // Keep these static file handlers in sync with app.yaml. - static := http.FileServer(http.Dir(root)) - http.Handle("/content/img/", static) - http.Handle("/static/", static) - imgDir := filepath.Join(root, "static", "img") - http.Handle("/favicon.ico", http.FileServer(http.Dir(imgDir))) + registerStatic(root) go func() { url := "http://" + httpAddr @@ -124,6 +123,16 @@ func main() { log.Fatal(http.ListenAndServe(httpAddr, nil)) } +// registerStatic registers handlers to serve static content +// from the directory root. +func registerStatic(root string) { + // Keep these static file handlers in sync with app.yaml. + http.Handle("/favicon.ico", http.FileServer(http.Dir(filepath.Join(root, "static", "img")))) + static := http.FileServer(http.Dir(root)) + http.Handle("/content/img/", static) + http.Handle("/static/", static) +} + // rootHandler returns a handler for all the requests except the ones for lessons. func rootHandler(w http.ResponseWriter, r *http.Request) { if err := renderUI(w); err != nil { @@ -146,7 +155,8 @@ func lessonHandler(w http.ResponseWriter, r *http.Request) { const localhostWarning = ` WARNING! WARNING! WARNING! -I appear to be listening on an address that is not localhost. +The tour server appears to be listening on an address that is +not localhost and is configured to run code snippets locally. Anyone with access to this address and port will have access to this machine as the user running gotour. @@ -210,7 +220,7 @@ func startBrowser(url string) bool { } // prepContent for the local tour simply returns the content as-is. -func prepContent(r io.Reader) io.Reader { return r } +var prepContent = func(r io.Reader) io.Reader { return r } // socketAddr returns the WebSocket handler address. -func socketAddr() string { return "ws://" + httpAddr + socketPath } +var socketAddr = func() string { return "ws://" + httpAddr + socketPath } |
