diff --git a/cmd/godoc/appinit.go b/cmd/godoc/appinit.go index 9604e9b87d..b0bcf3d264 100644 --- a/cmd/godoc/appinit.go +++ b/cmd/godoc/appinit.go @@ -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") } diff --git a/cmd/godoc/handlers.go b/cmd/godoc/handlers.go index f92b204a76..fbc0bfa5bd 100644 --- a/cmd/godoc/handlers.go +++ b/cmd/godoc/handlers.go @@ -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") +} diff --git a/cmd/godoc/main.go b/cmd/godoc/main.go index b2bb33671e..aea7367e0f 100644 --- a/cmd/godoc/main.go +++ b/cmd/godoc/main.go @@ -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"+ diff --git a/cmd/godoc/remotesearch.go b/cmd/godoc/remotesearch.go index 3052e489c0..f01d5c7acf 100644 --- a/cmd/godoc/remotesearch.go +++ b/cmd/godoc/remotesearch.go @@ -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 (