Skip to content

Commit 84d44bd

Browse files
committed
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 <[email protected]>
1 parent 1d6875d commit 84d44bd

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

.gcloudignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.gcloudignore
2+
.git

app.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
service: blog
2-
runtime: go
3-
api_version: go1.9
2+
runtime: go111
43

4+
env_variables:
5+
GOLANGORG_CHECK_COUNTRY: true
6+
7+
default_expiration: "7d"
8+
9+
# Keep these static file handlers in sync with blog.go.
10+
# They're here for improved latency across global regions.
511
handlers:
612
- url: /favicon.ico
713
static_files: static/favicon.ico
@@ -15,8 +21,4 @@ handlers:
1521
static_dir: static/fonts
1622
http_headers:
1723
Content-Type: application/font-woff
18-
- url: /.*
19-
script: _go_app
2024
secure: always
21-
22-
nobuild_files: ^(support|content)/

appengine.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build appengine
6-
75
// This file implements an App Engine blog server.
86

97
package main
108

119
import (
10+
"log"
1211
"net/http"
12+
"os"
1313

1414
"golang.org/x/tools/blog"
1515
)
1616

17-
func init() {
17+
func gaeMain() {
1818
config.ContentPath = "content/"
1919
config.TemplatePath = "template/"
2020
s, err := blog.NewServer(config)
2121
if err != nil {
22-
panic(err)
22+
log.Fatalln(err)
2323
}
2424
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
2525
w.Header().Set("Strict-Transport-Security", "max-age=31536000; preload")
2626
s.ServeHTTP(w, r)
2727
})
28+
port := os.Getenv("PORT")
29+
if port == "" {
30+
port = "8080"
31+
}
32+
log.Fatal(http.ListenAndServe(":"+port, nil))
2833
}

blog.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ func init() {
3737
}
3838
http.HandleFunc("/blog", redirect)
3939
http.HandleFunc("/blog/", redirect)
40-
http.Handle("/fonts/", http.FileServer(http.Dir("static")))
41-
http.Handle("/fonts.css", http.FileServer(http.Dir("static")))
40+
41+
// Keep these static file handlers in sync with app.yaml.
42+
static := http.FileServer(http.Dir("static"))
43+
http.Handle("/favicon.ico", static)
44+
http.Handle("/fonts.css", static)
45+
http.Handle("/fonts/", static)
46+
4247
http.Handle("/lib/godoc/", http.StripPrefix("/lib/godoc/", http.HandlerFunc(staticHandler)))
4348
}
4449

local.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build !appengine
6-
75
// This file implements a stand-alone blog server.
86

97
package main
@@ -81,6 +79,13 @@ func newServer(reload bool, staticPath string, config blog.Config) (http.Handler
8179

8280
func main() {
8381
flag.Parse()
82+
83+
if os.Getenv("GAE_ENV") == "standard" {
84+
log.Println("running in App Engine Standard mode")
85+
gaeMain()
86+
return
87+
}
88+
8489
config.ContentPath = *contentPath
8590
config.TemplatePath = *templatePath
8691
mux, err := newServer(*reload, *staticPath, config)
@@ -100,7 +105,7 @@ func main() {
100105
func reloadingBlogServer(w http.ResponseWriter, r *http.Request) {
101106
s, err := blog.NewServer(config)
102107
if err != nil {
103-
http.Error(w, err.Error(), 500)
108+
http.Error(w, err.Error(), http.StatusInternalServerError)
104109
return
105110
}
106111
s.ServeHTTP(w, r)

0 commit comments

Comments
 (0)