From 84d44bd73d1e20dd061849d296302a97052afdff Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Tue, 5 Mar 2019 18:09:30 -0500 Subject: 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 --- .gcloudignore | 2 ++ app.yaml | 14 ++++++++------ appengine.go | 13 +++++++++---- blog.go | 9 +++++++-- local.go | 11 ++++++++--- 5 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 .gcloudignore diff --git a/.gcloudignore b/.gcloudignore new file mode 100644 index 0000000..0030d4f --- /dev/null +++ b/.gcloudignore @@ -0,0 +1,2 @@ +.gcloudignore +.git diff --git a/app.yaml b/app.yaml index b6edeb7..1f250f6 100644 --- a/app.yaml +++ b/app.yaml @@ -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)) } diff --git a/blog.go b/blog.go index c1ea0eb..b1cd9bd 100644 --- a/blog.go +++ b/blog.go @@ -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))) } diff --git a/local.go b/local.go index e95dfea..1dba26c 100644 --- a/local.go +++ b/local.go @@ -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) -- cgit v1.2.3