1
0
mirror of https://github.com/golang/go synced 2024-09-30 20:28:32 -06:00

go.tools/cmd/godoc: update appinit.go to use new godoc packages

Also move some common code from main.go to handlers.go, and
exclude remotesearch.go from the appengine build (it is only
relevant to the command line interface).

R=bradfitz, dsymonds
CC=golang-dev
https://golang.org/cl/12001049
This commit is contained in:
Andrew Gerrand 2013-07-30 14:23:23 +10:00
parent 5ba51116ee
commit 253974207b
4 changed files with 66 additions and 74 deletions

View File

@ -12,18 +12,12 @@ package main
import (
"archive/zip"
"log"
"net/http"
"path"
)
func serveError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
w.WriteHeader(http.StatusNotFound)
servePage(w, Page{
Title: "File " + relpath,
Subtitle: relpath,
Body: applyTemplate(errorHTML, "errorHTML", err), // err may contain an absolute path!
})
}
"code.google.com/p/go.tools/godoc"
"code.google.com/p/go.tools/godoc/vfs"
"code.google.com/p/go.tools/godoc/vfs/zipfs"
)
func init() {
log.Println("initializing godoc ...")
@ -31,13 +25,7 @@ func init() {
log.Printf(".zip GOROOT = %s", zipGoroot)
log.Printf("index files = %s", indexFilenames)
// initialize flags for app engine
*goroot = path.Join("/", zipGoroot) // fsHttp paths are relative to '/'
*indexEnabled = true
*indexFiles = indexFilenames
*maxResults = 100 // reduce latency by limiting the number of fulltext search results
*indexThrottle = 0.3 // in case *indexFiles is empty (and thus the indexer is run)
*showPlayground = true
goroot := path.Join("/", zipGoroot) // fsHttp paths are relative to '/'
// read .zip file and set up file systems
const zipfile = zipFilename
@ -46,23 +34,25 @@ func init() {
log.Fatalf("%s: %s\n", zipfile, err)
}
// rc is never closed (app running forever)
fs.Bind("/", NewZipFS(rc, zipFilename), *goroot, bindReplace)
fs.Bind("/", zipfs.New(rc, zipFilename), goroot, vfs.BindReplace)
// initialize http handlers
readTemplates()
initHandlers()
registerPublicHandlers(http.DefaultServeMux)
corpus := godoc.NewCorpus(fs)
corpus.Verbose = false
corpus.IndexEnabled = true
corpus.IndexFiles = indexFilenames
// initialize default directory tree with corresponding timestamp.
initFSTree()
// Immediately update metadata.
updateMetadata()
// initialize search index
if *indexEnabled {
go indexer()
if err := corpus.Init(); err != nil {
log.Fatal(err)
}
pres = godoc.NewPresentation(corpus)
pres.TabWidth = 8
pres.ShowPlayground = true
pres.ShowExamples = true
pres.DeclLinks = true
readTemplates(pres)
registerHandlers(pres)
log.Println("godoc initialization complete")
}

View File

@ -13,9 +13,17 @@
package main
import (
"log"
"net/http"
"text/template"
"code.google.com/p/go.tools/godoc"
"code.google.com/p/go.tools/godoc/vfs"
)
var (
pres *godoc.Presentation
fs = vfs.NameSpace{}
)
func registerHandlers(pres *godoc.Presentation) {
@ -27,3 +35,38 @@ func registerHandlers(pres *godoc.Presentation) {
http.Handle("/robots.txt", pres.FileServer())
http.Handle("/", pres)
}
func readTemplate(name string) *template.Template {
if pres == nil {
panic("no global Presentation set yet")
}
path := "lib/godoc/" + name
// use underlying file system fs to read the template file
// (cannot use template ParseFile functions directly)
data, err := vfs.ReadFile(fs, path)
if err != nil {
log.Fatal("readTemplate: ", err)
}
// be explicit with errors (for app engine use)
t, err := template.New(name).Funcs(pres.FuncMap()).Parse(string(data))
if err != nil {
log.Fatal("readTemplate: ", err)
}
return t
}
func readTemplates(p *godoc.Presentation) {
// have to delay until after flags processing since paths depend on goroot
codewalkHTML = readTemplate("codewalk.html")
codewalkdirHTML = readTemplate("codewalkdir.html")
p.DirlistHTML = readTemplate("dirlist.html")
p.ErrorHTML = readTemplate("error.html")
p.ExampleHTML = readTemplate("example.html")
p.GodocHTML = readTemplate("godoc.html")
p.PackageHTML = readTemplate("package.html")
p.PackageText = readTemplate("package.txt")
p.SearchHTML = readTemplate("search.html")
p.SearchText = readTemplate("search.txt")
p.SearchDescXML = readTemplate("opensearch.xml")
}

View File

@ -101,49 +101,6 @@ var (
notesRx = flag.String("notes", "BUG", "regular expression matching note markers to show")
)
var (
pres *godoc.Presentation
fs = vfs.NameSpace{}
)
// ----------------------------------------------------------------------------
// Templates
func readTemplate(name string) *template.Template {
if pres == nil {
panic("no global Presentation set yet")
}
path := "lib/godoc/" + name
// use underlying file system fs to read the template file
// (cannot use template ParseFile functions directly)
data, err := vfs.ReadFile(fs, path)
if err != nil {
log.Fatal("readTemplate: ", err)
}
// be explicit with errors (for app engine use)
t, err := template.New(name).Funcs(pres.FuncMap()).Parse(string(data))
if err != nil {
log.Fatal("readTemplate: ", err)
}
return t
}
func readTemplates(p *godoc.Presentation) {
// have to delay until after flags processing since paths depend on goroot
codewalkHTML = readTemplate("codewalk.html")
codewalkdirHTML = readTemplate("codewalkdir.html")
p.DirlistHTML = readTemplate("dirlist.html")
p.ErrorHTML = readTemplate("error.html")
p.ExampleHTML = readTemplate("example.html")
p.GodocHTML = readTemplate("godoc.html")
p.PackageHTML = readTemplate("package.html")
p.PackageText = readTemplate("package.txt")
p.SearchHTML = readTemplate("search.html")
p.SearchText = readTemplate("search.txt")
p.SearchDescXML = readTemplate("opensearch.xml")
}
func usage() {
fmt.Fprintf(os.Stderr,
"usage: godoc package [name ...]\n"+

View File

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !appengine
package main
import (