1// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// godoc: Go Documentation Server
6
7// Web server tree:
8//
9//	http://godoc/		redirect to /pkg/
10//	http://godoc/src/	serve files from $GOROOT/src; .go gets pretty-printed
11//	http://godoc/cmd/	serve documentation about commands
12//	http://godoc/pkg/	serve documentation about packages
13//				(idea is if you say import "compress/zlib", you go to
14//				http://godoc/pkg/compress/zlib)
15//
16
17package main
18
19import (
20	"archive/zip"
21	"bytes"
22	"encoding/json"
23	_ "expvar" // to serve /debug/vars
24	"flag"
25	"fmt"
26	"go/build"
27	"io"
28	"log"
29	"net/http"
30	_ "net/http/pprof" // to serve /debug/pprof/*
31	"net/url"
32	"os"
33	"os/exec"
34	"path"
35	"path/filepath"
36	"regexp"
37	"runtime"
38	"strings"
39
40	"golang.org/x/tools/godoc"
41	"golang.org/x/tools/godoc/analysis"
42	"golang.org/x/tools/godoc/static"
43	"golang.org/x/tools/godoc/vfs"
44	"golang.org/x/tools/godoc/vfs/gatefs"
45	"golang.org/x/tools/godoc/vfs/mapfs"
46	"golang.org/x/tools/godoc/vfs/zipfs"
47	"golang.org/x/xerrors"
48)
49
50const defaultAddr = "localhost:6060" // default webserver address
51
52var (
53	// file system to serve
54	// (with e.g.: zip -r go.zip $GOROOT -i \*.go -i \*.html -i \*.css -i \*.js -i \*.txt -i \*.c -i \*.h -i \*.s -i \*.png -i \*.jpg -i \*.sh -i favicon.ico)
55	zipfile = flag.String("zip", "", "zip file providing the file system to serve; disabled if empty")
56
57	// file-based index
58	writeIndex = flag.Bool("write_index", false, "write index to a file; the file name must be specified with -index_files")
59
60	analysisFlag = flag.String("analysis", "", `comma-separated list of analyses to perform when in GOPATH mode (supported: type, pointer). See https://golang.org/lib/godoc/analysis/help.html`)
61
62	// network
63	httpAddr = flag.String("http", defaultAddr, "HTTP service address")
64
65	// layout control
66	urlFlag = flag.String("url", "", "print HTML for named URL")
67
68	verbose = flag.Bool("v", false, "verbose mode")
69
70	// file system roots
71	// TODO(gri) consider the invariant that goroot always end in '/'
72	goroot = flag.String("goroot", findGOROOT(), "Go root directory")
73
74	// layout control
75	showTimestamps = flag.Bool("timestamps", false, "show timestamps with directory listings")
76	templateDir    = flag.String("templates", "", "load templates/JS/CSS from disk in this directory")
77	showPlayground = flag.Bool("play", false, "enable playground")
78	declLinks      = flag.Bool("links", true, "link identifiers to their declarations")
79
80	// search index
81	indexEnabled  = flag.Bool("index", false, "enable search index")
82	indexFiles    = flag.String("index_files", "", "glob pattern specifying index files; if not empty, the index is read from these files in sorted order")
83	indexInterval = flag.Duration("index_interval", 0, "interval of indexing; 0 for default (5m), negative to only index once at startup")
84	maxResults    = flag.Int("maxresults", 10000, "maximum number of full text search results shown")
85	indexThrottle = flag.Float64("index_throttle", 0.75, "index throttle value; 0.0 = no time allocated, 1.0 = full throttle")
86
87	// source code notes
88	notesRx = flag.String("notes", "BUG", "regular expression matching note markers to show")
89)
90
91// An httpResponseRecorder is an http.ResponseWriter
92type httpResponseRecorder struct {
93	body   *bytes.Buffer
94	header http.Header
95	code   int
96}
97
98func (w *httpResponseRecorder) Header() http.Header         { return w.header }
99func (w *httpResponseRecorder) Write(b []byte) (int, error) { return w.body.Write(b) }
100func (w *httpResponseRecorder) WriteHeader(code int)        { w.code = code }
101
102func usage() {
103	fmt.Fprintf(os.Stderr, "usage: godoc -http="+defaultAddr+"\n")
104	flag.PrintDefaults()
105	os.Exit(2)
106}
107
108func loggingHandler(h http.Handler) http.Handler {
109	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
110		log.Printf("%s\t%s", req.RemoteAddr, req.URL)
111		h.ServeHTTP(w, req)
112	})
113}
114
115func handleURLFlag() {
116	// Try up to 10 fetches, following redirects.
117	urlstr := *urlFlag
118	for i := 0; i < 10; i++ {
119		// Prepare request.
120		u, err := url.Parse(urlstr)
121		if err != nil {
122			log.Fatal(err)
123		}
124		req := &http.Request{
125			URL: u,
126		}
127
128		// Invoke default HTTP handler to serve request
129		// to our buffering httpWriter.
130		w := &httpResponseRecorder{code: 200, header: make(http.Header), body: new(bytes.Buffer)}
131		http.DefaultServeMux.ServeHTTP(w, req)
132
133		// Return data, error, or follow redirect.
134		switch w.code {
135		case 200: // ok
136			os.Stdout.Write(w.body.Bytes())
137			return
138		case 301, 302, 303, 307: // redirect
139			redirect := w.header.Get("Location")
140			if redirect == "" {
141				log.Fatalf("HTTP %d without Location header", w.code)
142			}
143			urlstr = redirect
144		default:
145			log.Fatalf("HTTP error %d", w.code)
146		}
147	}
148	log.Fatalf("too many redirects")
149}
150
151func initCorpus(corpus *godoc.Corpus) {
152	err := corpus.Init()
153	if err != nil {
154		log.Fatal(err)
155	}
156}
157
158func main() {
159	flag.Usage = usage
160	flag.Parse()
161
162	// Check usage.
163	if flag.NArg() > 0 {
164		fmt.Fprintln(os.Stderr, `Unexpected arguments. Use "go doc" for command-line help output instead. For example, "go doc fmt.Printf".`)
165		usage()
166	}
167	if *httpAddr == "" && *urlFlag == "" && !*writeIndex {
168		fmt.Fprintln(os.Stderr, "At least one of -http, -url, or -write_index must be set to a non-zero value.")
169		usage()
170	}
171
172	// Set the resolved goroot.
173	vfs.GOROOT = *goroot
174
175	fsGate := make(chan bool, 20)
176
177	// Determine file system to use.
178	if *zipfile == "" {
179		// use file system of underlying OS
180		rootfs := gatefs.New(vfs.OS(*goroot), fsGate)
181		fs.Bind("/", rootfs, "/", vfs.BindReplace)
182	} else {
183		// use file system specified via .zip file (path separator must be '/')
184		rc, err := zip.OpenReader(*zipfile)
185		if err != nil {
186			log.Fatalf("%s: %s\n", *zipfile, err)
187		}
188		defer rc.Close() // be nice (e.g., -writeIndex mode)
189		fs.Bind("/", zipfs.New(rc, *zipfile), *goroot, vfs.BindReplace)
190	}
191	if *templateDir != "" {
192		fs.Bind("/lib/godoc", vfs.OS(*templateDir), "/", vfs.BindBefore)
193	} else {
194		fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
195	}
196
197	// Get the GOMOD value, use it to determine if godoc is being invoked in module mode.
198	goModFile, err := goMod()
199	if err != nil {
200		fmt.Fprintf(os.Stderr, "failed to determine go env GOMOD value: %v", err)
201		goModFile = "" // Fall back to GOPATH mode.
202	}
203
204	if goModFile != "" {
205		fmt.Printf("using module mode; GOMOD=%s\n", goModFile)
206
207		if *analysisFlag != "" {
208			fmt.Fprintln(os.Stderr, "The -analysis flag is supported only in GOPATH mode at this time.")
209			fmt.Fprintln(os.Stderr, "See https://golang.org/issue/34473.")
210			usage()
211		}
212
213		// Try to download dependencies that are not in the module cache in order to
214		// to show their documentation.
215		// This may fail if module downloading is disallowed (GOPROXY=off) or due to
216		// limited connectivity, in which case we print errors to stderr and show
217		// documentation only for packages that are available.
218		fillModuleCache(os.Stderr, goModFile)
219
220		// Determine modules in the build list.
221		mods, err := buildList(goModFile)
222		if err != nil {
223			fmt.Fprintf(os.Stderr, "failed to determine the build list of the main module: %v", err)
224			os.Exit(1)
225		}
226
227		// Bind module trees into Go root.
228		for _, m := range mods {
229			if m.Dir == "" {
230				// Module is not available in the module cache, skip it.
231				continue
232			}
233			dst := path.Join("/src", m.Path)
234			fs.Bind(dst, gatefs.New(vfs.OS(m.Dir), fsGate), "/", vfs.BindAfter)
235		}
236	} else {
237		fmt.Println("using GOPATH mode")
238
239		// Bind $GOPATH trees into Go root.
240		for _, p := range filepath.SplitList(build.Default.GOPATH) {
241			fs.Bind("/src", gatefs.New(vfs.OS(p), fsGate), "/src", vfs.BindAfter)
242		}
243	}
244
245	var typeAnalysis, pointerAnalysis bool
246	if *analysisFlag != "" {
247		for _, a := range strings.Split(*analysisFlag, ",") {
248			switch a {
249			case "type":
250				typeAnalysis = true
251			case "pointer":
252				pointerAnalysis = true
253			default:
254				log.Fatalf("unknown analysis: %s", a)
255			}
256		}
257	}
258
259	var corpus *godoc.Corpus
260	if goModFile != "" {
261		corpus = godoc.NewCorpus(moduleFS{fs})
262	} else {
263		corpus = godoc.NewCorpus(fs)
264	}
265	corpus.Verbose = *verbose
266	corpus.MaxResults = *maxResults
267	corpus.IndexEnabled = *indexEnabled
268	if *maxResults == 0 {
269		corpus.IndexFullText = false
270	}
271	corpus.IndexFiles = *indexFiles
272	corpus.IndexDirectory = func(dir string) bool {
273		return dir != "/pkg" && !strings.HasPrefix(dir, "/pkg/")
274	}
275	corpus.IndexThrottle = *indexThrottle
276	corpus.IndexInterval = *indexInterval
277	if *writeIndex || *urlFlag != "" {
278		corpus.IndexThrottle = 1.0
279		corpus.IndexEnabled = true
280		initCorpus(corpus)
281	} else {
282		go initCorpus(corpus)
283	}
284
285	// Initialize the version info before readTemplates, which saves
286	// the map value in a method value.
287	corpus.InitVersionInfo()
288
289	pres = godoc.NewPresentation(corpus)
290	pres.ShowTimestamps = *showTimestamps
291	pres.ShowPlayground = *showPlayground
292	pres.DeclLinks = *declLinks
293	if *notesRx != "" {
294		pres.NotesRx = regexp.MustCompile(*notesRx)
295	}
296
297	readTemplates(pres)
298	registerHandlers(pres)
299
300	if *writeIndex {
301		// Write search index and exit.
302		if *indexFiles == "" {
303			log.Fatal("no index file specified")
304		}
305
306		log.Println("initialize file systems")
307		*verbose = true // want to see what happens
308
309		corpus.UpdateIndex()
310
311		log.Println("writing index file", *indexFiles)
312		f, err := os.Create(*indexFiles)
313		if err != nil {
314			log.Fatal(err)
315		}
316		index, _ := corpus.CurrentIndex()
317		_, err = index.WriteTo(f)
318		if err != nil {
319			log.Fatal(err)
320		}
321
322		log.Println("done")
323		return
324	}
325
326	// Print content that would be served at the URL *urlFlag.
327	if *urlFlag != "" {
328		handleURLFlag()
329		return
330	}
331
332	var handler http.Handler = http.DefaultServeMux
333	if *verbose {
334		log.Printf("Go Documentation Server")
335		log.Printf("version = %s", runtime.Version())
336		log.Printf("address = %s", *httpAddr)
337		log.Printf("goroot = %s", *goroot)
338		switch {
339		case !*indexEnabled:
340			log.Print("search index disabled")
341		case *maxResults > 0:
342			log.Printf("full text index enabled (maxresults = %d)", *maxResults)
343		default:
344			log.Print("identifier search index enabled")
345		}
346		fs.Fprint(os.Stderr)
347		handler = loggingHandler(handler)
348	}
349
350	// Initialize search index.
351	if *indexEnabled {
352		go corpus.RunIndexer()
353	}
354
355	// Start type/pointer analysis.
356	if typeAnalysis || pointerAnalysis {
357		go analysis.Run(pointerAnalysis, &corpus.Analysis)
358	}
359
360	// Start http server.
361	if *verbose {
362		log.Println("starting HTTP server")
363	}
364	if err := http.ListenAndServe(*httpAddr, handler); err != nil {
365		log.Fatalf("ListenAndServe %s: %v", *httpAddr, err)
366	}
367}
368
369// goMod returns the go env GOMOD value in the current directory
370// by invoking the go command.
371//
372// GOMOD is documented at https://golang.org/cmd/go/#hdr-Environment_variables:
373//
374// 	The absolute path to the go.mod of the main module,
375// 	or the empty string if not using modules.
376//
377func goMod() (string, error) {
378	out, err := exec.Command("go", "env", "-json", "GOMOD").Output()
379	if ee := (*exec.ExitError)(nil); xerrors.As(err, &ee) {
380		return "", fmt.Errorf("go command exited unsuccessfully: %v\n%s", ee.ProcessState.String(), ee.Stderr)
381	} else if err != nil {
382		return "", err
383	}
384	var env struct {
385		GoMod string
386	}
387	err = json.Unmarshal(out, &env)
388	if err != nil {
389		return "", err
390	}
391	return env.GoMod, nil
392}
393
394// fillModuleCache does a best-effort attempt to fill the module cache
395// with all dependencies of the main module in the current directory
396// by invoking the go command. Module download logs are streamed to w.
397// If there are any problems encountered, they are also written to w.
398// It should only be used when operating in module mode.
399//
400// See https://golang.org/cmd/go/#hdr-Download_modules_to_local_cache.
401func fillModuleCache(w io.Writer, goMod string) {
402	if goMod == os.DevNull {
403		// No module requirements, nothing to do.
404		return
405	}
406
407	cmd := exec.Command("go", "mod", "download", "-json")
408	var out bytes.Buffer
409	cmd.Stdout = &out
410	cmd.Stderr = w
411	err := cmd.Run()
412	if ee := (*exec.ExitError)(nil); xerrors.As(err, &ee) && ee.ExitCode() == 1 {
413		// Exit code 1 from this command means there were some
414		// non-empty Error values in the output. Print them to w.
415		fmt.Fprintf(w, "documentation for some packages is not shown:\n")
416		for dec := json.NewDecoder(&out); ; {
417			var m struct {
418				Path    string // Module path.
419				Version string // Module version.
420				Error   string // Error loading module.
421			}
422			err := dec.Decode(&m)
423			if err == io.EOF {
424				break
425			} else if err != nil {
426				fmt.Fprintf(w, "error decoding JSON object from go mod download -json: %v\n", err)
427				continue
428			}
429			if m.Error == "" {
430				continue
431			}
432			fmt.Fprintf(w, "\tmodule %s@%s is not in the module cache and there was a problem downloading it: %s\n", m.Path, m.Version, m.Error)
433		}
434	} else if err != nil {
435		fmt.Fprintf(w, "there was a problem filling module cache: %v\n", err)
436	}
437}
438
439// buildList determines the build list in the current directory
440// by invoking the go command. It should only be used when operating
441// in module mode.
442//
443// See https://golang.org/cmd/go/#hdr-The_main_module_and_the_build_list.
444func buildList(goMod string) ([]mod, error) {
445	if goMod == os.DevNull {
446		// Empty build list.
447		return nil, nil
448	}
449
450	out, err := exec.Command("go", "list", "-m", "-json", "all").Output()
451	if ee := (*exec.ExitError)(nil); xerrors.As(err, &ee) {
452		return nil, fmt.Errorf("go command exited unsuccessfully: %v\n%s", ee.ProcessState.String(), ee.Stderr)
453	} else if err != nil {
454		return nil, err
455	}
456	var mods []mod
457	for dec := json.NewDecoder(bytes.NewReader(out)); ; {
458		var m mod
459		err := dec.Decode(&m)
460		if err == io.EOF {
461			break
462		} else if err != nil {
463			return nil, err
464		}
465		mods = append(mods, m)
466	}
467	return mods, nil
468}
469
470type mod struct {
471	Path string // Module path.
472	Dir  string // Directory holding files for this module, if any.
473}
474
475// moduleFS is a vfs.FileSystem wrapper used when godoc is running
476// in module mode. It's needed so that packages inside modules are
477// considered to be third party.
478//
479// It overrides the RootType method of the underlying filesystem
480// and implements it using a heuristic based on the import path.
481// If the first element of the import path does not contain a dot,
482// that package is considered to be inside GOROOT. If it contains
483// a dot, then that package is considered to be third party.
484//
485// TODO(dmitshur): The RootType abstraction works well when GOPATH
486// workspaces are bound at their roots, but scales poorly in the
487// general case. It should be replaced by a more direct solution
488// for determining whether a package is third party or not.
489//
490type moduleFS struct{ vfs.FileSystem }
491
492func (moduleFS) RootType(path string) vfs.RootType {
493	if !strings.HasPrefix(path, "/src/") {
494		return ""
495	}
496	domain := path[len("/src/"):]
497	if i := strings.Index(domain, "/"); i >= 0 {
498		domain = domain[:i]
499	}
500	if !strings.Contains(domain, ".") {
501		// No dot in the first element of import path
502		// suggests this is a package in GOROOT.
503		return vfs.RootTypeGoRoot
504	} else {
505		// A dot in the first element of import path
506		// suggests this is a third party package.
507		return vfs.RootTypeGoPath
508	}
509}
510func (fs moduleFS) String() string { return "module(" + fs.FileSystem.String() + ")" }
511