blob: 18934113b4a26c8c45296053690f212f221d46b8 [file] [log] [blame]
Brad Fitzpatrickca3319f2013-07-17 17:17:12 +10001// 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
5package godoc
6
7import (
8 "log"
9 "net/http"
10 "runtime"
11 "text/template"
12)
13
14// Page describes the contents of the top-level godoc webpage.
15type Page struct {
16 Title string
17 Tabtitle string
18 Subtitle string
19 Query string
20 Body []byte
21
22 // filled in by servePage
23 SearchBox bool
24 Playground bool
25 Version string
26}
27
28var (
29 DirlistHTML,
30 ErrorHTML,
31 ExampleHTML,
32 GodocHTML,
33 PackageHTML,
34 PackageText,
35 SearchHTML,
36 SearchText,
37 SearchDescXML *template.Template
38)
39
Brad Fitzpatrick4fc63232013-07-18 09:52:45 +100040func (p *Presentation) ServePage(w http.ResponseWriter, page Page) {
Brad Fitzpatrickca3319f2013-07-17 17:17:12 +100041 if page.Tabtitle == "" {
42 page.Tabtitle = page.Title
43 }
Brad Fitzpatrick4fc63232013-07-18 09:52:45 +100044 page.SearchBox = p.Corpus.IndexEnabled
Brad Fitzpatrick5395cfe2013-07-18 13:14:09 +100045 page.Playground = p.ShowPlayground
Brad Fitzpatrickca3319f2013-07-17 17:17:12 +100046 page.Version = runtime.Version()
47 if err := GodocHTML.Execute(w, page); err != nil && err != http.ErrBodyNotAllowed {
48 // Only log if there's an error that's not about writing on HEAD requests.
49 // See Issues 5451 and 5454.
50 log.Printf("GodocHTML.Execute: %s", err)
51 }
52}
53
Brad Fitzpatrick4fc63232013-07-18 09:52:45 +100054func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
Brad Fitzpatrickca3319f2013-07-17 17:17:12 +100055 w.WriteHeader(http.StatusNotFound)
Brad Fitzpatrick4fc63232013-07-18 09:52:45 +100056 p.ServePage(w, Page{
Brad Fitzpatrickca3319f2013-07-17 17:17:12 +100057 Title: "File " + relpath,
58 Subtitle: relpath,
59 Body: applyTemplate(ErrorHTML, "errorHTML", err), // err may contain an absolute path!
60 })
61}