Brad Fitzpatrick | ca3319f | 2013-07-17 17:17:12 +1000 | [diff] [blame] | 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 | "log" |
| 9 | "net/http" |
| 10 | "runtime" |
| 11 | "text/template" |
| 12 | ) |
| 13 | |
| 14 | // Page describes the contents of the top-level godoc webpage. |
| 15 | type 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 | |
| 28 | var ( |
| 29 | DirlistHTML, |
| 30 | ErrorHTML, |
| 31 | ExampleHTML, |
| 32 | GodocHTML, |
| 33 | PackageHTML, |
| 34 | PackageText, |
| 35 | SearchHTML, |
| 36 | SearchText, |
| 37 | SearchDescXML *template.Template |
| 38 | ) |
| 39 | |
Brad Fitzpatrick | 4fc6323 | 2013-07-18 09:52:45 +1000 | [diff] [blame] | 40 | func (p *Presentation) ServePage(w http.ResponseWriter, page Page) { |
Brad Fitzpatrick | ca3319f | 2013-07-17 17:17:12 +1000 | [diff] [blame] | 41 | if page.Tabtitle == "" { |
| 42 | page.Tabtitle = page.Title |
| 43 | } |
Brad Fitzpatrick | 4fc6323 | 2013-07-18 09:52:45 +1000 | [diff] [blame] | 44 | page.SearchBox = p.Corpus.IndexEnabled |
Brad Fitzpatrick | 5395cfe | 2013-07-18 13:14:09 +1000 | [diff] [blame^] | 45 | page.Playground = p.ShowPlayground |
Brad Fitzpatrick | ca3319f | 2013-07-17 17:17:12 +1000 | [diff] [blame] | 46 | 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 Fitzpatrick | 4fc6323 | 2013-07-18 09:52:45 +1000 | [diff] [blame] | 54 | func (p *Presentation) ServeError(w http.ResponseWriter, r *http.Request, relpath string, err error) { |
Brad Fitzpatrick | ca3319f | 2013-07-17 17:17:12 +1000 | [diff] [blame] | 55 | w.WriteHeader(http.StatusNotFound) |
Brad Fitzpatrick | 4fc6323 | 2013-07-18 09:52:45 +1000 | [diff] [blame] | 56 | p.ServePage(w, Page{ |
Brad Fitzpatrick | ca3319f | 2013-07-17 17:17:12 +1000 | [diff] [blame] | 57 | Title: "File " + relpath, |
| 58 | Subtitle: relpath, |
| 59 | Body: applyTemplate(ErrorHTML, "errorHTML", err), // err may contain an absolute path! |
| 60 | }) |
| 61 | } |