2009-10-30 11:58:53 -06:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
|
|
|
// godoc: Go Documentation Server
|
|
|
|
|
|
|
|
// Web server tree:
|
|
|
|
//
|
|
|
|
// http://godoc/ main landing page
|
|
|
|
// http://godoc/doc/ serve from $GOROOT/doc - spec, mem, tutorial, etc.
|
|
|
|
// http://godoc/src/ serve files from $GOROOT/src; .go gets pretty-printed
|
|
|
|
// http://godoc/cmd/ serve documentation about commands (TODO)
|
|
|
|
// http://godoc/pkg/ serve documentation about packages
|
|
|
|
// (idea is if you say import "compress/zlib", you go to
|
|
|
|
// http://godoc/pkg/compress/zlib)
|
|
|
|
//
|
|
|
|
// Command-line interface:
|
|
|
|
//
|
|
|
|
// godoc packagepath [name ...]
|
|
|
|
//
|
|
|
|
// godoc compress/zlib
|
|
|
|
// - prints doc for package compress/zlib
|
|
|
|
// godoc crypto/block Cipher NewCMAC
|
|
|
|
// - prints doc for Cipher and NewCMAC in package crypto/block
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:33:31 -07:00
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"http"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
2010-02-19 17:23:19 -07:00
|
|
|
pathutil "path"
|
2010-03-19 13:46:43 -06:00
|
|
|
"rpc"
|
2010-03-19 16:20:20 -06:00
|
|
|
"runtime"
|
2009-12-15 16:33:31 -07:00
|
|
|
"time"
|
2009-10-30 11:58:53 -06:00
|
|
|
)
|
|
|
|
|
2010-03-19 13:46:43 -06:00
|
|
|
const (
|
|
|
|
defaultAddr = ":6060" // default webserver address
|
|
|
|
golangAddr = "golang.org:http"
|
|
|
|
)
|
|
|
|
|
2009-10-30 11:58:53 -06:00
|
|
|
var (
|
|
|
|
// periodic sync
|
2010-03-16 17:45:54 -06:00
|
|
|
syncCmd = flag.String("sync", "", "sync command; disabled if empty")
|
|
|
|
syncMin = flag.Int("sync_minutes", 0, "sync interval in minutes; disabled if <= 0")
|
2009-12-15 16:33:31 -07:00
|
|
|
syncDelay delayTime // actual sync delay in minutes; usually syncDelay == syncMin, but delay may back off exponentially
|
2009-10-30 11:58:53 -06:00
|
|
|
|
2010-03-19 13:46:43 -06:00
|
|
|
// network
|
|
|
|
httpAddr = flag.String("http", "", "HTTP service address (e.g., '"+defaultAddr+"')")
|
|
|
|
serverAddr = flag.String("server", "", "webserver address for command line searches")
|
2009-10-30 11:58:53 -06:00
|
|
|
|
|
|
|
// layout control
|
2010-03-10 16:22:22 -07:00
|
|
|
html = flag.Bool("html", false, "print HTML in command-line mode")
|
2010-03-10 16:41:36 -07:00
|
|
|
genAST = flag.Bool("src", false, "print exported source in command-line mode")
|
2010-03-19 13:46:43 -06:00
|
|
|
|
|
|
|
// command-line searches
|
|
|
|
query = flag.Bool("q", false, "arguments are considered search queries")
|
2009-10-30 11:58:53 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2010-02-18 18:40:50 -07:00
|
|
|
func serveError(c *http.Conn, r *http.Request, relpath string, err os.Error) {
|
|
|
|
contents := applyTemplate(errorHTML, "errorHTML", err) // err may contain an absolute path!
|
|
|
|
servePage(c, "File "+relpath, "", contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-01 11:33:16 -07:00
|
|
|
func exec(c *http.Conn, args []string) (status int) {
|
2009-12-15 16:33:31 -07:00
|
|
|
r, w, err := os.Pipe()
|
2009-10-30 11:58:53 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:33:31 -07:00
|
|
|
log.Stderrf("os.Pipe(): %v\n", err)
|
|
|
|
return 2
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
bin := args[0]
|
|
|
|
fds := []*os.File{nil, w, w}
|
2009-10-30 11:58:53 -06:00
|
|
|
if *verbose {
|
2009-11-09 13:07:39 -07:00
|
|
|
log.Stderrf("executing %v", args)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2010-03-19 16:20:20 -06:00
|
|
|
pid, err := os.ForkExec(bin, args, os.Environ(), *goroot, fds)
|
2009-12-15 16:33:31 -07:00
|
|
|
defer r.Close()
|
|
|
|
w.Close()
|
2009-10-30 11:58:53 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:33:31 -07:00
|
|
|
log.Stderrf("os.ForkExec(%q): %v\n", bin, err)
|
|
|
|
return 2
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, r)
|
|
|
|
wait, err := os.Wait(pid, 0)
|
2009-10-30 11:58:53 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:33:31 -07:00
|
|
|
os.Stderr.Write(buf.Bytes())
|
|
|
|
log.Stderrf("os.Wait(%d, 0): %v\n", pid, err)
|
|
|
|
return 2
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
status = wait.ExitStatus()
|
2009-11-05 23:25:46 -07:00
|
|
|
if !wait.Exited() || status > 1 {
|
2009-12-15 16:33:31 -07:00
|
|
|
os.Stderr.Write(buf.Bytes())
|
|
|
|
log.Stderrf("executing %v failed (exit status = %d)", args, status)
|
|
|
|
return
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if *verbose {
|
2009-11-09 13:07:39 -07:00
|
|
|
os.Stderr.Write(buf.Bytes())
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
if c != nil {
|
2009-12-15 16:33:31 -07:00
|
|
|
c.SetHeader("content-type", "text/plain; charset=utf-8")
|
|
|
|
c.Write(buf.Bytes())
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-03 20:40:26 -07:00
|
|
|
// Maximum directory depth, adjust as needed.
|
2009-11-05 23:25:46 -07:00
|
|
|
const maxDirDepth = 24
|
2009-11-02 23:44:01 -07:00
|
|
|
|
2009-10-30 11:58:53 -06:00
|
|
|
func dosync(c *http.Conn, r *http.Request) {
|
2009-12-15 16:33:31 -07:00
|
|
|
args := []string{"/bin/sh", "-c", *syncCmd}
|
2009-11-01 11:33:16 -07:00
|
|
|
switch exec(c, args) {
|
|
|
|
case 0:
|
2009-11-02 10:25:39 -07:00
|
|
|
// sync succeeded and some files have changed;
|
2009-11-02 23:44:01 -07:00
|
|
|
// update package tree.
|
|
|
|
// TODO(gri): The directory tree may be temporarily out-of-sync.
|
|
|
|
// Consider keeping separate time stamps so the web-
|
|
|
|
// page can indicate this discrepancy.
|
2010-03-19 16:20:20 -06:00
|
|
|
fsTree.set(newDirectory(*goroot, maxDirDepth))
|
2009-12-15 16:33:31 -07:00
|
|
|
fallthrough
|
2009-11-01 11:33:16 -07:00
|
|
|
case 1:
|
2009-11-02 10:25:39 -07:00
|
|
|
// sync failed because no files changed;
|
|
|
|
// don't change the package tree
|
2009-12-15 16:33:31 -07:00
|
|
|
syncDelay.set(*syncMin) // revert to regular sync schedule
|
2009-11-01 11:33:16 -07:00
|
|
|
default:
|
|
|
|
// sync failed because of an error - back off exponentially, but try at least once a day
|
2009-11-09 22:09:34 -07:00
|
|
|
syncDelay.backoff(24 * 60)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func usage() {
|
|
|
|
fmt.Fprintf(os.Stderr,
|
2009-12-09 17:54:07 -07:00
|
|
|
"usage: godoc package [name ...]\n"+
|
2010-03-19 13:46:43 -06:00
|
|
|
" godoc -http="+defaultAddr+"\n")
|
2009-12-15 16:33:31 -07:00
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func loggingHandler(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(c *http.Conn, req *http.Request) {
|
2009-12-15 16:33:31 -07:00
|
|
|
log.Stderrf("%s\t%s", c.RemoteAddr, req.URL)
|
|
|
|
h.ServeHTTP(c, req)
|
2009-11-09 13:07:39 -07:00
|
|
|
})
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-19 23:48:08 -06:00
|
|
|
func remoteLookup(query string) (result *Result, err os.Error) {
|
2010-03-19 13:46:43 -06:00
|
|
|
var client *rpc.Client
|
|
|
|
if *serverAddr != "" {
|
|
|
|
// try server only
|
|
|
|
client, err = rpc.DialHTTP("tcp", *serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// try local default client first, followed by golang.org
|
|
|
|
client, err = rpc.DialHTTP("tcp", defaultAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Stderrf("trying %s (no local webserver found at %s)", golangAddr, defaultAddr)
|
|
|
|
client, err = rpc.Dial("tcp", golangAddr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-19 23:48:08 -06:00
|
|
|
result = new(Result)
|
2010-03-19 13:46:43 -06:00
|
|
|
err = client.Call("IndexServer.Lookup", &Query{query}, result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-30 11:58:53 -06:00
|
|
|
func main() {
|
2009-12-15 16:33:31 -07:00
|
|
|
flag.Usage = usage
|
|
|
|
flag.Parse()
|
2009-10-30 11:58:53 -06:00
|
|
|
|
2009-11-09 00:42:00 -07:00
|
|
|
// Check usage: either server and no args, or command line and args
|
2010-03-19 13:46:43 -06:00
|
|
|
if (*httpAddr != "") != (flag.NArg() == 0) {
|
2009-11-09 13:07:39 -07:00
|
|
|
usage()
|
2009-11-09 00:42:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if *tabwidth < 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
log.Exitf("negative tabwidth %d", *tabwidth)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2010-02-16 13:49:41 -07:00
|
|
|
initHandlers()
|
2009-12-15 16:33:31 -07:00
|
|
|
readTemplates()
|
2009-10-30 11:58:53 -06:00
|
|
|
|
2010-03-19 13:46:43 -06:00
|
|
|
if *httpAddr != "" {
|
2009-11-08 22:46:20 -07:00
|
|
|
// HTTP server mode.
|
2009-12-15 16:33:31 -07:00
|
|
|
var handler http.Handler = http.DefaultServeMux
|
2009-10-30 11:58:53 -06:00
|
|
|
if *verbose {
|
2009-12-15 16:33:31 -07:00
|
|
|
log.Stderrf("Go Documentation Server\n")
|
2010-03-19 16:20:20 -06:00
|
|
|
log.Stderrf("version = %s\n", runtime.Version())
|
2010-03-19 13:46:43 -06:00
|
|
|
log.Stderrf("address = %s\n", *httpAddr)
|
2010-03-19 16:20:20 -06:00
|
|
|
log.Stderrf("goroot = %s\n", *goroot)
|
2009-12-15 16:33:31 -07:00
|
|
|
log.Stderrf("tabwidth = %d\n", *tabwidth)
|
2010-02-16 12:20:55 -07:00
|
|
|
if !fsMap.IsEmpty() {
|
|
|
|
log.Stderr("user-defined mapping:")
|
|
|
|
fsMap.Fprint(os.Stderr)
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
handler = loggingHandler(handler)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
registerPublicHandlers(http.DefaultServeMux)
|
2009-10-30 11:58:53 -06:00
|
|
|
if *syncCmd != "" {
|
2009-11-09 13:07:39 -07:00
|
|
|
http.Handle("/debug/sync", http.HandlerFunc(dosync))
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2009-11-03 20:40:26 -07:00
|
|
|
// Initialize directory tree with corresponding timestamp.
|
2009-11-02 23:44:01 -07:00
|
|
|
// Do it in two steps:
|
|
|
|
// 1) set timestamp right away so that the indexer is kicked on
|
2009-12-15 16:33:31 -07:00
|
|
|
fsTree.set(nil)
|
2009-11-03 20:40:26 -07:00
|
|
|
// 2) compute initial directory tree in a goroutine so that launch is quick
|
2010-03-19 16:20:20 -06:00
|
|
|
go func() { fsTree.set(newDirectory(*goroot, maxDirDepth)) }()
|
2009-10-30 11:58:53 -06:00
|
|
|
|
|
|
|
// Start sync goroutine, if enabled.
|
|
|
|
if *syncCmd != "" && *syncMin > 0 {
|
2009-12-15 16:33:31 -07:00
|
|
|
syncDelay.set(*syncMin) // initial sync delay
|
2009-10-30 11:58:53 -06:00
|
|
|
go func() {
|
|
|
|
for {
|
2009-12-15 16:33:31 -07:00
|
|
|
dosync(nil, nil)
|
|
|
|
delay, _ := syncDelay.get()
|
2009-10-30 11:58:53 -06:00
|
|
|
if *verbose {
|
2009-11-09 13:07:39 -07:00
|
|
|
log.Stderrf("next sync in %dmin", delay.(int))
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
time.Sleep(int64(delay.(int)) * 60e9)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
}()
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start indexing goroutine.
|
2009-12-15 16:33:31 -07:00
|
|
|
go indexer()
|
2009-10-30 11:58:53 -06:00
|
|
|
|
2009-11-02 10:25:39 -07:00
|
|
|
// The server may have been restarted; always wait 1sec to
|
|
|
|
// give the forking server a chance to shut down and release
|
|
|
|
// the http port.
|
|
|
|
// TODO(gri): Do we still need this?
|
2009-12-15 16:33:31 -07:00
|
|
|
time.Sleep(1e9)
|
2009-11-02 10:25:39 -07:00
|
|
|
|
2010-03-19 13:46:43 -06:00
|
|
|
// Register index server.
|
|
|
|
rpc.Register(new(IndexServer))
|
|
|
|
rpc.HandleHTTP()
|
|
|
|
|
2009-10-30 11:58:53 -06:00
|
|
|
// Start http server.
|
2010-03-19 13:46:43 -06:00
|
|
|
if err := http.ListenAndServe(*httpAddr, handler); err != nil {
|
|
|
|
log.Exitf("ListenAndServe %s: %v", *httpAddr, err)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2010-03-19 13:46:43 -06:00
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2009-11-02 10:25:39 -07:00
|
|
|
// Command line mode.
|
2009-10-30 11:58:53 -06:00
|
|
|
if *html {
|
2009-12-01 10:15:05 -07:00
|
|
|
packageText = packageHTML
|
2010-03-19 13:46:43 -06:00
|
|
|
searchText = packageHTML
|
|
|
|
}
|
|
|
|
|
|
|
|
if *query {
|
|
|
|
// Command-line queries.
|
|
|
|
for i := 0; i < flag.NArg(); i++ {
|
|
|
|
result, err := remoteLookup(flag.Arg(i))
|
|
|
|
if err != nil {
|
|
|
|
log.Exitf("remoteLookup: %s", err)
|
|
|
|
}
|
2010-03-19 23:48:08 -06:00
|
|
|
os.Stdout.Write(result.Result)
|
2010-03-19 13:46:43 -06:00
|
|
|
}
|
|
|
|
return
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2010-02-19 17:23:19 -07:00
|
|
|
// determine paths
|
|
|
|
path := flag.Arg(0)
|
|
|
|
if len(path) > 0 && path[0] == '.' {
|
|
|
|
// assume cwd; don't assume -goroot
|
|
|
|
cwd, _ := os.Getwd() // ignore errors
|
|
|
|
path = pathutil.Join(cwd, path)
|
|
|
|
}
|
|
|
|
relpath := path
|
|
|
|
abspath := path
|
|
|
|
if len(path) > 0 && path[0] != '/' {
|
|
|
|
abspath = absolutePath(path, pkgHandler.fsRoot)
|
|
|
|
} else {
|
|
|
|
relpath = relativePath(path)
|
|
|
|
}
|
|
|
|
|
2010-03-12 19:16:21 -07:00
|
|
|
// TODO(gri): Provide a mechanism (flag?) to select a package
|
|
|
|
// if there are multiple packages in a directory.
|
|
|
|
info := pkgHandler.getPageInfo(abspath, relpath, "", *genAST, true)
|
2009-11-03 20:40:26 -07:00
|
|
|
|
2010-03-10 16:22:22 -07:00
|
|
|
if info.PAst == nil && info.PDoc == nil && info.Dirs == nil {
|
2009-11-03 20:40:26 -07:00
|
|
|
// try again, this time assume it's a command
|
2010-02-19 17:23:19 -07:00
|
|
|
if len(path) > 0 && path[0] != '/' {
|
|
|
|
abspath = absolutePath(path, cmdHandler.fsRoot)
|
|
|
|
}
|
2010-03-12 19:16:21 -07:00
|
|
|
info = cmdHandler.getPageInfo(abspath, relpath, "", false, false)
|
2009-11-03 20:40:26 -07:00
|
|
|
}
|
2009-10-30 11:58:53 -06:00
|
|
|
|
|
|
|
if info.PDoc != nil && flag.NArg() > 1 {
|
2009-12-15 16:33:31 -07:00
|
|
|
args := flag.Args()
|
|
|
|
info.PDoc.Filter(args[1:])
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := packageText.Execute(info, os.Stdout); err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
log.Stderrf("packageText.Execute: %s", err)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
}
|