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:
parent
4df6ae9fad
commit
1330b289ad
|
@ -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()
|
||||||
|
}
|
|
@ -281,6 +281,7 @@ func sanitizeFunc(src string) string {
|
||||||
type PageInfo struct {
|
type PageInfo struct {
|
||||||
Dirname string // directory containing the package
|
Dirname string // directory containing the package
|
||||||
Err error // error or nil
|
Err error // error or nil
|
||||||
|
Share bool // show share button on examples
|
||||||
|
|
||||||
// package info
|
// package info
|
||||||
FSet *token.FileSet // nil if no package documentation
|
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 {
|
err := p.ExampleHTML.Execute(&buf, struct {
|
||||||
Name, Doc, Code, Play, Output string
|
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 {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ type Page struct {
|
||||||
Subtitle string
|
Subtitle string
|
||||||
Query string
|
Query string
|
||||||
Body []byte
|
Body []byte
|
||||||
|
Share bool
|
||||||
|
|
||||||
// filled in by servePage
|
// filled in by servePage
|
||||||
SearchBox bool
|
SearchBox bool
|
||||||
|
@ -39,5 +40,19 @@ func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpat
|
||||||
Title: "File " + relpath,
|
Title: "File " + relpath,
|
||||||
Subtitle: relpath,
|
Subtitle: relpath,
|
||||||
Body: applyTemplate(p.ErrorHTML, "errorHTML", err), // err may contain an absolute path!
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -126,6 +126,7 @@ func (p *Presentation) HandleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
Tabtitle: query,
|
Tabtitle: query,
|
||||||
Query: query,
|
Query: query,
|
||||||
Body: body.Bytes(),
|
Body: body.Bytes(),
|
||||||
|
Share: allowShare(r),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -300,11 +300,13 @@ func (h *handlerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
info.TypeInfoIndex[ti.Name] = i
|
info.TypeInfoIndex[ti.Name] = i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
info.Share = allowShare(r)
|
||||||
h.p.ServePage(w, Page{
|
h.p.ServePage(w, Page{
|
||||||
Title: title,
|
Title: title,
|
||||||
Tabtitle: tabtitle,
|
Tabtitle: tabtitle,
|
||||||
Subtitle: subtitle,
|
Subtitle: subtitle,
|
||||||
Body: applyTemplate(h.p.PackageHTML, "packageHTML", info),
|
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,
|
Title: title + " " + relpath,
|
||||||
Tabtitle: relpath,
|
Tabtitle: relpath,
|
||||||
Body: buf.Bytes(),
|
Body: buf.Bytes(),
|
||||||
|
Share: allowShare(r),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -606,6 +609,7 @@ func (p *Presentation) serveDirectory(w http.ResponseWriter, r *http.Request, ab
|
||||||
Title: "Directory " + relpath,
|
Title: "Directory " + relpath,
|
||||||
Tabtitle: relpath,
|
Tabtitle: relpath,
|
||||||
Body: applyTemplate(p.DirlistHTML, "dirlistHTML", list),
|
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)
|
log.Printf("decoding metadata %s: %v", relpath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
page := Page{
|
||||||
|
Title: meta.Title,
|
||||||
|
Subtitle: meta.Subtitle,
|
||||||
|
Share: allowShare(r),
|
||||||
|
}
|
||||||
|
|
||||||
// evaluate as template if indicated
|
// evaluate as template if indicated
|
||||||
if meta.Template {
|
if meta.Template {
|
||||||
tmpl, err := template.New("main").Funcs(p.TemplateFuncs()).Parse(string(src))
|
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
|
return
|
||||||
}
|
}
|
||||||
var buf bytes.Buffer
|
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)
|
log.Printf("executing template %s: %v", relpath, err)
|
||||||
p.ServeError(w, r, relpath, err)
|
p.ServeError(w, r, relpath, err)
|
||||||
return
|
return
|
||||||
|
@ -655,11 +665,8 @@ func (p *Presentation) ServeHTMLDoc(w http.ResponseWriter, r *http.Request, absp
|
||||||
src = buf.Bytes()
|
src = buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
p.ServePage(w, Page{
|
page.Body = src
|
||||||
Title: meta.Title,
|
p.ServePage(w, page)
|
||||||
Subtitle: meta.Subtitle,
|
|
||||||
Body: src,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Presentation) ServeFile(w http.ResponseWriter, r *http.Request) {
|
func (p *Presentation) ServeFile(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -13,7 +13,9 @@
|
||||||
<div class="buttons">
|
<div class="buttons">
|
||||||
<a class="run" title="Run this code [shift-enter]">Run</a>
|
<a class="run" title="Run this code [shift-enter]">Run</a>
|
||||||
<a class="fmt" title="Format this code">Format</a>
|
<a class="fmt" title="Format this code">Format</a>
|
||||||
|
{{if $.Share}}
|
||||||
<a class="share" title="Share this code">Share</a>
|
<a class="share" title="Share this code">Share</a>
|
||||||
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{else}}
|
{{else}}
|
||||||
|
|
|
@ -55,7 +55,9 @@ func main() {
|
||||||
<div class="buttons">
|
<div class="buttons">
|
||||||
<a class="run" title="Run this code [shift-enter]">Run</a>
|
<a class="run" title="Run this code [shift-enter]">Run</a>
|
||||||
<a class="fmt" title="Format this code">Format</a>
|
<a class="fmt" title="Format this code">Format</a>
|
||||||
|
{{if $.Share}}
|
||||||
<a class="share" title="Share this code">Share</a>
|
<a class="share" title="Share this code">Share</a>
|
||||||
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
@ -441,7 +441,9 @@ var Files = map[string]string{
|
||||||
<div class="buttons">
|
<div class="buttons">
|
||||||
<a class="run" title="Run this code [shift-enter]">Run</a>
|
<a class="run" title="Run this code [shift-enter]">Run</a>
|
||||||
<a class="fmt" title="Format this code">Format</a>
|
<a class="fmt" title="Format this code">Format</a>
|
||||||
|
{{if $.Share}}
|
||||||
<a class="share" title="Share this code">Share</a>
|
<a class="share" title="Share this code">Share</a>
|
||||||
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{else}}
|
{{else}}
|
||||||
|
@ -513,7 +515,9 @@ func main() {
|
||||||
<div class="buttons">
|
<div class="buttons">
|
||||||
<a class="run" title="Run this code [shift-enter]">Run</a>
|
<a class="run" title="Run this code [shift-enter]">Run</a>
|
||||||
<a class="fmt" title="Format this code">Format</a>
|
<a class="fmt" title="Format this code">Format</a>
|
||||||
|
{{if $.Share}}
|
||||||
<a class="share" title="Share this code">Share</a>
|
<a class="share" title="Share this code">Share</a>
|
||||||
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
Loading…
Reference in New Issue