diff options
author | Dmitri Shuralyov <dmitshur@golang.org> | 2019-03-05 18:09:30 -0500 |
---|---|---|
committer | Dmitri Shuralyov <dmitshur@golang.org> | 2019-03-07 18:57:43 +0000 |
commit | 84d44bd73d1e20dd061849d296302a97052afdff (patch) | |
tree | a2dfdb286464b2b3d700c8a610041c6974451a7f | |
parent | 1d6875d7d19a4a23fec704623a87b523d1153a5f (diff) |
blog: deploy with App Engine Standard on Go 1.11
This change upgrades the deployment of blog 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 blog is being run
in App Engine mode.
Set an environment variable in app.yaml to configure the
x/tools/godoc/golangorgenv package appropriately.
Modify the static file server to also serve /favicon.ico,
but keep static file handlers in app.yaml for improved latency
across global regions.
Updates golang/go#30486
Change-Id: I63ca78a075d94d43a40f0b963b5f6d0d8270c34e
Reviewed-on: https://go-review.googlesource.com/c/blog/+/165460
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r-- | .gcloudignore | 2 | ||||
-rw-r--r-- | app.yaml | 14 | ||||
-rw-r--r-- | appengine.go | 13 | ||||
-rw-r--r-- | blog.go | 9 | ||||
-rw-r--r-- | local.go | 11 |
5 files changed, 34 insertions, 15 deletions
diff --git a/.gcloudignore b/.gcloudignore new file mode 100644 index 0000000..0030d4f --- /dev/null +++ b/.gcloudignore @@ -0,0 +1,2 @@ +.gcloudignore +.git @@ -1,7 +1,13 @@ service: blog -runtime: go -api_version: go1.9 +runtime: go111 +env_variables: + GOLANGORG_CHECK_COUNTRY: true + +default_expiration: "7d" + +# Keep these static file handlers in sync with blog.go. +# They're here for improved latency across global regions. handlers: - url: /favicon.ico static_files: static/favicon.ico @@ -15,8 +21,4 @@ handlers: static_dir: static/fonts http_headers: Content-Type: application/font-woff -- url: /.* - script: _go_app secure: always - -nobuild_files: ^(support|content)/ diff --git a/appengine.go b/appengine.go index 288247e..3b65bc7 100644 --- a/appengine.go +++ b/appengine.go @@ -2,27 +2,32 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build appengine - // This file implements an App Engine blog server. package main import ( + "log" "net/http" + "os" "golang.org/x/tools/blog" ) -func init() { +func gaeMain() { config.ContentPath = "content/" config.TemplatePath = "template/" s, err := blog.NewServer(config) if err != nil { - panic(err) + log.Fatalln(err) } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Strict-Transport-Security", "max-age=31536000; preload") s.ServeHTTP(w, r) }) + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + log.Fatal(http.ListenAndServe(":"+port, nil)) } @@ -37,8 +37,13 @@ func init() { } http.HandleFunc("/blog", redirect) http.HandleFunc("/blog/", redirect) - http.Handle("/fonts/", http.FileServer(http.Dir("static"))) - http.Handle("/fonts.css", http.FileServer(http.Dir("static"))) + + // Keep these static file handlers in sync with app.yaml. + static := http.FileServer(http.Dir("static")) + http.Handle("/favicon.ico", static) + http.Handle("/fonts.css", static) + http.Handle("/fonts/", static) + http.Handle("/lib/godoc/", http.StripPrefix("/lib/godoc/", http.HandlerFunc(staticHandler))) } @@ -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 - // This file implements a stand-alone blog server. package main @@ -81,6 +79,13 @@ func newServer(reload bool, staticPath string, config blog.Config) (http.Handler func main() { flag.Parse() + + if os.Getenv("GAE_ENV") == "standard" { + log.Println("running in App Engine Standard mode") + gaeMain() + return + } + config.ContentPath = *contentPath config.TemplatePath = *templatePath mux, err := newServer(*reload, *staticPath, config) @@ -100,7 +105,7 @@ func main() { func reloadingBlogServer(w http.ResponseWriter, r *http.Request) { s, err := blog.NewServer(config) if err != nil { - http.Error(w, err.Error(), 500) + http.Error(w, err.Error(), http.StatusInternalServerError) return } s.ServeHTTP(w, r) |