1
0
mirror of https://github.com/golang/go synced 2024-11-18 04:04:49 -07:00

godoc/env: replace with golangorgenv

This change replaces the env package with a new golangorgenv package.

The previous env package existed primarily to configure the godoc
instance that was running golang.org. By now, the golang.org website
has been factored out to x/website, which has its own env package,
but ends up still using this env package indirectly via x/tools/godoc.

The goal of this change is to make env available for other services
that run on subdomains of golang.org, so they can continue to safely
rely on the x/tools/playground, which will be modified in the next
commit to also use the new golangorgenv.

The golangorgenv package replaces the IsProd function with a more
specific one. Start using it in packages x/tools/{,cmd}/godoc. Also,
re-arrange the order of checks to give the host suffix check higher
priority than the environment variable check. This way, if the
environment variable isn't set, the host suffix check gets a chance
to run.

When getting the value of "X-AppEngine-Country" header, use its
canonical format "X-Appengine-Country" to avoid an allocation.
This does not change behavior.

Updates golang/go#30486

Change-Id: I97b47211a45ca0351f31fcb4fa6d408a4b0c4c7c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/165459
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Dmitri Shuralyov 2019-03-05 16:56:02 -05:00
parent 69e0dcfa11
commit bd17c084df
3 changed files with 19 additions and 16 deletions

View File

@ -21,7 +21,7 @@ import (
"text/template"
"golang.org/x/tools/godoc"
"golang.org/x/tools/godoc/env"
"golang.org/x/tools/godoc/golangorgenv"
"golang.org/x/tools/godoc/redirect"
"golang.org/x/tools/godoc/vfs"
)
@ -40,7 +40,7 @@ type hostEnforcerHandler struct {
}
func (h hostEnforcerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !env.EnforceHosts() {
if !golangorgenv.EnforceHosts() {
h.h.ServeHTTP(w, r)
return
}

View File

@ -2,9 +2,9 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package env provides environment information for the godoc server running on
// golang.org.
package env
// Package golangorgenv provides environment information for programs running at
// golang.org and its subdomains.
package golangorgenv
import (
"log"
@ -13,14 +13,13 @@ import (
)
var (
isProd = boolEnv("GODOC_PROD")
enforceHosts = boolEnv("GODOC_ENFORCE_HOSTS")
checkCountry = boolEnv("GOLANGORG_CHECK_COUNTRY")
enforceHosts = boolEnv("GOLANGORG_ENFORCE_HOSTS")
)
// IsProd reports whether the server is running in its production configuration
// on golang.org.
func IsProd() bool {
return isProd
// CheckCountry reports whether country restrictions should be enforced.
func CheckCountry() bool {
return checkCountry
}
// EnforceHosts reports whether host filtering should be enforced.
@ -31,6 +30,8 @@ func EnforceHosts() bool {
func boolEnv(key string) bool {
v := os.Getenv(key)
if v == "" {
// TODO(dmitshur): In the future, consider detecting if running in App Engine,
// and if so, making the environment variables mandatory rather than optional.
return false
}
b, err := strconv.ParseBool(v)

View File

@ -11,7 +11,7 @@ import (
"runtime"
"strings"
"golang.org/x/tools/godoc/env"
"golang.org/x/tools/godoc/golangorgenv"
)
// Page describes the contents of the top-level godoc webpage.
@ -62,17 +62,19 @@ func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpat
})
}
// googleCN reports whether request r is considered
// to be served from golang.google.cn.
func googleCN(r *http.Request) bool {
if r.FormValue("googlecn") != "" {
return true
}
if !env.IsProd() {
return false
}
if strings.HasSuffix(r.Host, ".cn") {
return true
}
switch r.Header.Get("X-AppEngine-Country") {
if !golangorgenv.CheckCountry() {
return false
}
switch r.Header.Get("X-Appengine-Country") {
case "", "ZZ", "CN":
return true
}