1
0
mirror of https://github.com/golang/go synced 2024-09-30 14:28:33 -06:00

godoc: hide and block share functionality from specific countries

This will permit us to serve *.golang.org to Chinese users.

Change-Id: I5217753aa67931522c7e6be106477534c99a20b2
Reviewed-on: https://go-review.googlesource.com/14194
Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
Andrew Gerrand 2015-09-02 09:49:30 +10:00
parent 4df6ae9fad
commit 1330b289ad
8 changed files with 53 additions and 7 deletions

13
godoc/appengine.go Normal file
View File

@ -0,0 +1,13 @@
// Copyright 2015 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.
// +build appengine
package godoc
import "appengine"
func init() {
onAppengine = !appengine.IsDevAppServer()
}

View File

@ -281,6 +281,7 @@ func sanitizeFunc(src string) string {
type PageInfo struct {
Dirname string // directory containing the package
Err error // error or nil
Share bool // show share button on examples
// package info
FSet *token.FileSet // nil if no package documentation
@ -490,7 +491,8 @@ func (p *Presentation) example_htmlFunc(info *PageInfo, funcName string) string
err := p.ExampleHTML.Execute(&buf, struct {
Name, Doc, Code, Play, Output string
}{eg.Name, eg.Doc, code, play, out})
Share bool
}{eg.Name, eg.Doc, code, play, out, info.Share})
if err != nil {
log.Print(err)
}

View File

@ -16,6 +16,7 @@ type Page struct {
Subtitle string
Query string
Body []byte
Share bool
// filled in by servePage
SearchBox bool
@ -39,5 +40,19 @@ func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpat
Title: "File " + relpath,
Subtitle: relpath,
Body: applyTemplate(p.ErrorHTML, "errorHTML", err), // err may contain an absolute path!
Share: allowShare(r),
})
}
var onAppengine = false // overriden in appengine.go when on app engine
func allowShare(r *http.Request) bool {
if !onAppengine {
return true
}
switch r.Header.Get("X-AppEngine-Country") {
case "", "ZZ", "HK", "CN", "RC":
return false
}
return true
}

View File

@ -126,6 +126,7 @@ func (p *Presentation) HandleSearch(w http.ResponseWriter, r *http.Request) {
Tabtitle: query,
Query: query,
Body: body.Bytes(),
Share: allowShare(r),
})
}

View File

@ -300,11 +300,13 @@ func (h *handlerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
info.TypeInfoIndex[ti.Name] = i
}
info.Share = allowShare(r)
h.p.ServePage(w, Page{
Title: title,
Tabtitle: tabtitle,
Subtitle: subtitle,
Body: applyTemplate(h.p.PackageHTML, "packageHTML", info),
Share: info.Share,
})
}
@ -546,6 +548,7 @@ func (p *Presentation) serveTextFile(w http.ResponseWriter, r *http.Request, abs
Title: title + " " + relpath,
Tabtitle: relpath,
Body: buf.Bytes(),
Share: allowShare(r),
})
}
@ -606,6 +609,7 @@ func (p *Presentation) serveDirectory(w http.ResponseWriter, r *http.Request, ab
Title: "Directory " + relpath,
Tabtitle: relpath,
Body: applyTemplate(p.DirlistHTML, "dirlistHTML", list),
Share: allowShare(r),
})
}
@ -631,6 +635,12 @@ func (p *Presentation) ServeHTMLDoc(w http.ResponseWriter, r *http.Request, absp
log.Printf("decoding metadata %s: %v", relpath, err)
}
page := Page{
Title: meta.Title,
Subtitle: meta.Subtitle,
Share: allowShare(r),
}
// evaluate as template if indicated
if meta.Template {
tmpl, err := template.New("main").Funcs(p.TemplateFuncs()).Parse(string(src))
@ -640,7 +650,7 @@ func (p *Presentation) ServeHTMLDoc(w http.ResponseWriter, r *http.Request, absp
return
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, nil); err != nil {
if err := tmpl.Execute(&buf, page); err != nil {
log.Printf("executing template %s: %v", relpath, err)
p.ServeError(w, r, relpath, err)
return
@ -655,11 +665,8 @@ func (p *Presentation) ServeHTMLDoc(w http.ResponseWriter, r *http.Request, absp
src = buf.Bytes()
}
p.ServePage(w, Page{
Title: meta.Title,
Subtitle: meta.Subtitle,
Body: src,
})
page.Body = src
p.ServePage(w, page)
}
func (p *Presentation) ServeFile(w http.ResponseWriter, r *http.Request) {

View File

@ -13,7 +13,9 @@
<div class="buttons">
<a class="run" title="Run this code [shift-enter]">Run</a>
<a class="fmt" title="Format this code">Format</a>
{{if $.Share}}
<a class="share" title="Share this code">Share</a>
{{end}}
</div>
</div>
{{else}}

View File

@ -55,7 +55,9 @@ func main() {
<div class="buttons">
<a class="run" title="Run this code [shift-enter]">Run</a>
<a class="fmt" title="Format this code">Format</a>
{{if $.Share}}
<a class="share" title="Share this code">Share</a>
{{end}}
</div>
</div>
{{end}}

View File

@ -441,7 +441,9 @@ var Files = map[string]string{
<div class="buttons">
<a class="run" title="Run this code [shift-enter]">Run</a>
<a class="fmt" title="Format this code">Format</a>
{{if $.Share}}
<a class="share" title="Share this code">Share</a>
{{end}}
</div>
</div>
{{else}}
@ -513,7 +515,9 @@ func main() {
<div class="buttons">
<a class="run" title="Run this code [shift-enter]">Run</a>
<a class="fmt" title="Format this code">Format</a>
{{if $.Share}}
<a class="share" title="Share this code">Share</a>
{{end}}
</div>
</div>
{{end}}