mirror of
https://github.com/golang/go
synced 2024-11-05 15:46:11 -07:00
bd17c084df
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>
83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package godoc
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"golang.org/x/tools/godoc/golangorgenv"
|
|
)
|
|
|
|
// Page describes the contents of the top-level godoc webpage.
|
|
type Page struct {
|
|
Title string
|
|
Tabtitle string
|
|
Subtitle string
|
|
SrcPath string
|
|
Query string
|
|
Body []byte
|
|
GoogleCN bool // page is being served from golang.google.cn
|
|
TreeView bool // page needs to contain treeview related js and css
|
|
|
|
// filled in by ServePage
|
|
SearchBox bool
|
|
Playground bool
|
|
Version string
|
|
GoogleAnalytics string
|
|
}
|
|
|
|
func (p *Presentation) ServePage(w http.ResponseWriter, page Page) {
|
|
if page.Tabtitle == "" {
|
|
page.Tabtitle = page.Title
|
|
}
|
|
page.SearchBox = p.Corpus.IndexEnabled
|
|
page.Playground = p.ShowPlayground
|
|
page.Version = runtime.Version()
|
|
page.GoogleAnalytics = p.GoogleAnalytics
|
|
applyTemplateToResponseWriter(w, p.GodocHTML, page)
|
|
}
|
|
|
|
func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
if perr, ok := err.(*os.PathError); ok {
|
|
rel, err := filepath.Rel(runtime.GOROOT(), perr.Path)
|
|
if err != nil {
|
|
perr.Path = "REDACTED"
|
|
} else {
|
|
perr.Path = filepath.Join("$GOROOT", rel)
|
|
}
|
|
}
|
|
p.ServePage(w, Page{
|
|
Title: "File " + relpath,
|
|
Subtitle: relpath,
|
|
Body: applyTemplate(p.ErrorHTML, "errorHTML", err),
|
|
GoogleCN: googleCN(r),
|
|
GoogleAnalytics: p.GoogleAnalytics,
|
|
})
|
|
}
|
|
|
|
// 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 strings.HasSuffix(r.Host, ".cn") {
|
|
return true
|
|
}
|
|
if !golangorgenv.CheckCountry() {
|
|
return false
|
|
}
|
|
switch r.Header.Get("X-Appengine-Country") {
|
|
case "", "ZZ", "CN":
|
|
return true
|
|
}
|
|
return false
|
|
}
|