mirror of
https://github.com/golang/go
synced 2024-11-06 06:36:20 -07:00
c75e7e6983
- This shaves off 12.8KB (unzipped) / 3.8KB (zipped) downloadable data and around 6ms of javascript parse time when the files are not injected. - Also, conditionally rendering the callgraph html only when callgraph is actually present. - Removed the "text/javascript" attribute as it is the default value now. Change-Id: I70c030bdda2a054c181b2b3a693842bafed88d1a Reviewed-on: https://go-review.googlesource.com/c/115875 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
81 lines
1.9 KiB
Go
81 lines
1.9 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/env"
|
|
)
|
|
|
|
// 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,
|
|
})
|
|
}
|
|
|
|
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") {
|
|
case "", "ZZ", "CN":
|
|
return true
|
|
}
|
|
return false
|
|
}
|