From 80d38231f758dcfe0cfcc8474e9e5dbb4296159b Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 19 Jul 2013 14:02:03 +1000 Subject: [PATCH] godoc: move search from cmd to the package R=golang-dev, adg CC=golang-dev https://golang.org/cl/11540045 --- cmd/godoc/codewalk.go | 9 +++++++++ cmd/godoc/handlers.go | 29 +++++++++++++++++++++++++++++ cmd/godoc/main.go | 43 +------------------------------------------ godoc/pres.go | 2 ++ godoc/search.go | 13 +++++++++++++ godoc/server.go | 12 ------------ 6 files changed, 54 insertions(+), 54 deletions(-) create mode 100644 cmd/godoc/handlers.go diff --git a/cmd/godoc/codewalk.go b/cmd/godoc/codewalk.go index f9e161b893..c93cefe7e1 100644 --- a/cmd/godoc/codewalk.go +++ b/cmd/godoc/codewalk.go @@ -13,6 +13,7 @@ package main import ( + "bytes" "encoding/xml" "errors" "fmt" @@ -95,6 +96,14 @@ func redirect(w http.ResponseWriter, r *http.Request) (redirected bool) { return } +func applyTemplate(t *template.Template, name string, data interface{}) []byte { + var buf bytes.Buffer + if err := t.Execute(&buf, data); err != nil { + log.Printf("%s.Execute: %s", name, err) + } + return buf.Bytes() +} + // A Codewalk represents a single codewalk read from an XML file. type Codewalk struct { Title string `xml:"title,attr"` diff --git a/cmd/godoc/handlers.go b/cmd/godoc/handlers.go new file mode 100644 index 0000000000..f92b204a76 --- /dev/null +++ b/cmd/godoc/handlers.go @@ -0,0 +1,29 @@ +// Copyright 2010 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. + +// The /doc/codewalk/ tree is synthesized from codewalk descriptions, +// files named $GOROOT/doc/codewalk/*.xml. +// For an example and a description of the format, see +// http://golang.org/doc/codewalk/codewalk or run godoc -http=:6060 +// and see http://localhost:6060/doc/codewalk/codewalk . +// That page is itself a codewalk; the source code for it is +// $GOROOT/doc/codewalk/codewalk.xml. + +package main + +import ( + "net/http" + + "code.google.com/p/go.tools/godoc" +) + +func registerHandlers(pres *godoc.Presentation) { + if pres == nil { + panic("nil Presentation") + } + http.HandleFunc("/doc/codewalk/", codewalk) + http.Handle("/doc/play/", pres.FileServer()) + http.Handle("/robots.txt", pres.FileServer()) + http.Handle("/", pres) +} diff --git a/cmd/godoc/main.go b/cmd/godoc/main.go index 148b36d2c2..b2bb33671e 100644 --- a/cmd/godoc/main.go +++ b/cmd/godoc/main.go @@ -106,18 +106,6 @@ var ( fs = vfs.NameSpace{} ) -func registerPublicHandlers(mux *http.ServeMux) { - if pres == nil { - panic("nil Presentation") - } - mux.HandleFunc("/doc/codewalk/", codewalk) - mux.Handle("/doc/play/", pres.FileServer()) - mux.HandleFunc("/search", pres.HandleSearch) - mux.Handle("/robots.txt", pres.FileServer()) - mux.HandleFunc("/opensearch.xml", serveSearchDesc) - mux.Handle("/", pres) -} - // ---------------------------------------------------------------------------- // Templates @@ -156,32 +144,6 @@ func readTemplates(p *godoc.Presentation) { p.SearchDescXML = readTemplate("opensearch.xml") } -// ---------------------------------------------------------------------------- -// Files - -func applyTemplate(t *template.Template, name string, data interface{}) []byte { - var buf bytes.Buffer - if err := t.Execute(&buf, data); err != nil { - log.Printf("%s.Execute: %s", name, err) - } - return buf.Bytes() -} - -func serveSearchDesc(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/opensearchdescription+xml") - data := map[string]interface{}{ - "BaseURL": fmt.Sprintf("http://%s", r.Host), - } - if err := pres.SearchDescXML.Execute(w, &data); err != nil && err != http.ErrBodyNotAllowed { - // Only log if there's an error that's not about writing on HEAD requests. - // See Issues 5451 and 5454. - log.Printf("searchDescXML.Execute: %s", err) - } -} - -// ---------------------------------------------------------------------------- -// Packages - func usage() { fmt.Fprintf(os.Stderr, "usage: godoc package [name ...]\n"+ @@ -224,8 +186,6 @@ func makeRx(names []string) (rx *regexp.Regexp) { } func handleURLFlag() { - registerPublicHandlers(http.DefaultServeMux) - // Try up to 10 fetches, following redirects. urlstr := *urlFlag for i := 0; i < 10; i++ { @@ -317,6 +277,7 @@ func main() { } readTemplates(pres) + registerHandlers(pres) if *writeIndex { // Write search index and exit. @@ -371,8 +332,6 @@ func main() { handler = loggingHandler(handler) } - registerPublicHandlers(http.DefaultServeMux) - // Initialize search index. if *indexEnabled { go corpus.RunIndexer() diff --git a/godoc/pres.go b/godoc/pres.go index 3a8b99a3c1..29b4432b51 100644 --- a/godoc/pres.go +++ b/godoc/pres.go @@ -73,6 +73,8 @@ func NewPresentation(c *Corpus) *Presentation { p.cmdHandler.registerWithMux(p.mux) p.pkgHandler.registerWithMux(p.mux) p.mux.HandleFunc("/", p.ServeFile) + p.mux.HandleFunc("/search", p.HandleSearch) + p.mux.HandleFunc("/opensearch.xml", p.serveSearchDesc) return p } diff --git a/godoc/search.go b/godoc/search.go index a50de5f5ce..63a4f1f9e3 100644 --- a/godoc/search.go +++ b/godoc/search.go @@ -6,6 +6,7 @@ package godoc import ( "fmt" + "log" "net/http" "regexp" "strings" @@ -97,3 +98,15 @@ func (p *Presentation) HandleSearch(w http.ResponseWriter, r *http.Request) { Body: applyTemplate(p.SearchHTML, "searchHTML", result), }) } + +func (p *Presentation) serveSearchDesc(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/opensearchdescription+xml") + data := map[string]interface{}{ + "BaseURL": fmt.Sprintf("http://%s", r.Host), + } + if err := p.SearchDescXML.Execute(w, &data); err != nil && err != http.ErrBodyNotAllowed { + // Only log if there's an error that's not about writing on HEAD requests. + // See Issues 5451 and 5454. + log.Printf("searchDescXML.Execute: %s", err) + } +} diff --git a/godoc/server.go b/godoc/server.go index b3b05d8051..9744a5043b 100644 --- a/godoc/server.go +++ b/godoc/server.go @@ -567,18 +567,6 @@ func (p *Presentation) serveFile(w http.ResponseWriter, r *http.Request) { p.fileServer.ServeHTTP(w, r) } -func (p *Presentation) serveSearchDesc(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/opensearchdescription+xml") - data := map[string]interface{}{ - "BaseURL": fmt.Sprintf("http://%s", r.Host), - } - if err := p.SearchDescXML.Execute(w, &data); err != nil && err != http.ErrBodyNotAllowed { - // Only log if there's an error that's not about writing on HEAD requests. - // See Issues 5451 and 5454. - log.Printf("searchDescXML.Execute: %s", err) - } -} - func (p *Presentation) ServeText(w http.ResponseWriter, text []byte) { w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.Write(text)