1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:54:40 -07: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 ( import (
"archive/zip" "archive/zip"
"log" "log"
"net/http"
"path" "path"
)
func serveError(w http.ResponseWriter, r *http.Request, relpath string, err error) { "code.google.com/p/go.tools/godoc"
w.WriteHeader(http.StatusNotFound) "code.google.com/p/go.tools/godoc/vfs"
servePage(w, Page{ "code.google.com/p/go.tools/godoc/vfs/zipfs"
Title: "File " + relpath, )
Subtitle: relpath,
Body: applyTemplate(errorHTML, "errorHTML", err), // err may contain an absolute path!
})
}
func init() { func init() {
log.Println("initializing godoc ...") log.Println("initializing godoc ...")
@ -31,13 +25,7 @@ func init() {
log.Printf(".zip GOROOT = %s", zipGoroot) log.Printf(".zip GOROOT = %s", zipGoroot)
log.Printf("index files = %s", indexFilenames) log.Printf("index files = %s", indexFilenames)
// initialize flags for app engine goroot := path.Join("/", zipGoroot) // fsHttp paths are relative to '/'
*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
// read .zip file and set up file systems // read .zip file and set up file systems
const zipfile = zipFilename const zipfile = zipFilename
@ -46,23 +34,25 @@ func init() {
log.Fatalf("%s: %s\n", zipfile, err) log.Fatalf("%s: %s\n", zipfile, err)
} }
// rc is never closed (app running forever) // 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 corpus := godoc.NewCorpus(fs)
readTemplates() corpus.Verbose = false
initHandlers() corpus.IndexEnabled = true
registerPublicHandlers(http.DefaultServeMux) corpus.IndexFiles = indexFilenames
// initialize default directory tree with corresponding timestamp. if err := corpus.Init(); err != nil {
initFSTree() log.Fatal(err)
// Immediately update metadata.
updateMetadata()
// initialize search index
if *indexEnabled {
go indexer()
} }
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") log.Println("godoc initialization complete")
} }

View File

@ -13,9 +13,17 @@
package main package main
import ( import (
"log"
"net/http" "net/http"
"text/template"
"code.google.com/p/go.tools/godoc" "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) { func registerHandlers(pres *godoc.Presentation) {
@ -27,3 +35,38 @@ func registerHandlers(pres *godoc.Presentation) {
http.Handle("/robots.txt", pres.FileServer()) http.Handle("/robots.txt", pres.FileServer())
http.Handle("/", pres) 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") 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() { func usage() {
fmt.Fprintf(os.Stderr, fmt.Fprintf(os.Stderr,
"usage: godoc package [name ...]\n"+ "usage: godoc package [name ...]\n"+

View File

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