github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/godoc/page.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package godoc
     6  
     7  import (
     8  	"net/http"
     9  	"runtime"
    10  )
    11  
    12  // Page describes the contents of the top-level godoc webpage.
    13  type Page struct {
    14  	Title    string
    15  	Tabtitle string
    16  	Subtitle string
    17  	Query    string
    18  	Body     []byte
    19  	Share    bool
    20  
    21  	// filled in by servePage
    22  	SearchBox  bool
    23  	Playground bool
    24  	Version    string
    25  }
    26  
    27  func (p *Presentation) ServePage(w http.ResponseWriter, page Page) {
    28  	if page.Tabtitle == "" {
    29  		page.Tabtitle = page.Title
    30  	}
    31  	page.SearchBox = p.Corpus.IndexEnabled
    32  	page.Playground = p.ShowPlayground
    33  	page.Version = runtime.Version()
    34  	applyTemplateToResponseWriter(w, p.GodocHTML, page)
    35  }
    36  
    37  func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
    38  	w.WriteHeader(http.StatusNotFound)
    39  	p.ServePage(w, Page{
    40  		Title:    "File " + relpath,
    41  		Subtitle: relpath,
    42  		Body:     applyTemplate(p.ErrorHTML, "errorHTML", err), // err may contain an absolute path!
    43  		Share:    allowShare(r),
    44  	})
    45  }
    46  
    47  var onAppengine = false // overriden in appengine.go when on app engine
    48  
    49  func allowShare(r *http.Request) bool {
    50  	if !onAppengine {
    51  		return true
    52  	}
    53  	switch r.Header.Get("X-AppEngine-Country") {
    54  	case "", "ZZ", "CN":
    55  		return false
    56  	}
    57  	return true
    58  }