cmd/golangorg: move googleCN code out of internal/godoc
Change-Id: I24140d73ca375cd624d852a2653c0a8bb0af9e2d Reviewed-on: https://go-review.googlesource.com/c/website/+/293492 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
Родитель
b24263deb8
Коммит
bcc60328ef
|
@ -99,6 +99,7 @@ func main() {
|
|||
if *notesRx != "" {
|
||||
pres.NotesRx = regexp.MustCompile(*notesRx)
|
||||
}
|
||||
pres.GoogleCN = googleCN
|
||||
|
||||
readTemplates(pres)
|
||||
mux := registerHandlers(pres)
|
||||
|
|
|
@ -12,9 +12,6 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/website/internal/godoc/golangorgenv"
|
||||
)
|
||||
|
||||
// Page describes the contents of the top-level godoc webpage.
|
||||
|
@ -57,26 +54,7 @@ func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpat
|
|||
Title: "File " + relpath,
|
||||
Subtitle: relpath,
|
||||
Body: applyTemplate(p.ErrorHTML, "errorHTML", err),
|
||||
GoogleCN: googleCN(r),
|
||||
GoogleCN: p.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
|
||||
}
|
||||
|
|
|
@ -68,6 +68,10 @@ type Presentation struct {
|
|||
// the query string highlighted.
|
||||
URLForSrcQuery func(src, query string, line int) string
|
||||
|
||||
// GoogleCN reports whether this request should be marked GoogleCN.
|
||||
// If the function is nil, no requests are marked GoogleCN.
|
||||
GoogleCN func(*http.Request) bool
|
||||
|
||||
// GoogleAnalytics optionally adds Google Analytics via the provided
|
||||
// tracking ID to each page.
|
||||
GoogleAnalytics string
|
||||
|
@ -137,3 +141,7 @@ func (p *Presentation) GetPkgPageInfo(abspath, relpath string, mode PageInfoMode
|
|||
func (p *Presentation) GetCmdPageInfo(abspath, relpath string, mode PageInfoMode) *PageInfo {
|
||||
return p.cmdHandler.GetPageInfo(abspath, relpath, mode, "", "")
|
||||
}
|
||||
|
||||
func (p *Presentation) googleCN(r *http.Request) bool {
|
||||
return p.GoogleCN != nil && p.GoogleCN(r)
|
||||
}
|
||||
|
|
|
@ -319,7 +319,7 @@ func (h *handlerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
tabtitle = "Commands"
|
||||
}
|
||||
|
||||
info.GoogleCN = googleCN(r)
|
||||
info.GoogleCN = h.p.googleCN(r)
|
||||
var body []byte
|
||||
if info.Dirname == "/src" {
|
||||
body = applyTemplate(h.p.PackageRootHTML, "packageRootHTML", info)
|
||||
|
@ -588,7 +588,7 @@ func (p *Presentation) serveTextFile(w http.ResponseWriter, r *http.Request, abs
|
|||
SrcPath: relpath,
|
||||
Tabtitle: relpath,
|
||||
Body: buf.Bytes(),
|
||||
GoogleCN: googleCN(r),
|
||||
GoogleCN: p.googleCN(r),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -659,7 +659,7 @@ func (p *Presentation) serveDirectory(w http.ResponseWriter, r *http.Request, ab
|
|||
SrcPath: relpath,
|
||||
Tabtitle: relpath,
|
||||
Body: applyTemplate(p.DirlistHTML, "dirlistHTML", info),
|
||||
GoogleCN: googleCN(r),
|
||||
GoogleCN: p.googleCN(r),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -696,7 +696,7 @@ func (p *Presentation) ServeHTMLDoc(w http.ResponseWriter, r *http.Request, absp
|
|||
page := Page{
|
||||
Title: meta.Title,
|
||||
Subtitle: meta.Subtitle,
|
||||
GoogleCN: googleCN(r),
|
||||
GoogleCN: p.googleCN(r),
|
||||
}
|
||||
|
||||
// evaluate as template if indicated
|
||||
|
|
Загрузка…
Ссылка в новой задаче