1
0
mirror of https://github.com/golang/go synced 2024-09-30 16:18:35 -06:00

godoc: move search from cmd to the package

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/11540045
This commit is contained in:
Brad Fitzpatrick 2013-07-19 14:02:03 +10:00
parent 5b72c43fc0
commit 80d38231f7
6 changed files with 54 additions and 54 deletions

View File

@ -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"`

29
cmd/godoc/handlers.go Normal file
View File

@ -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)
}

View File

@ -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()

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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)