| // 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. |
| |
| // golangorg: The Go Website (golang.org) |
| |
| // Web server tree: |
| // |
| // https://golang.org/ main landing page |
| // https://golang.org/doc/ serve from content/doc, then $GOROOT/doc. spec, mem, etc. |
| // https://golang.org/src/ serve files from $GOROOT/src; .go gets pretty-printed |
| // https://golang.org/cmd/ serve documentation about commands |
| // https://golang.org/pkg/ serve documentation about packages |
| // (idea is if you say import "compress/zlib", you go to |
| // https://golang.org/pkg/compress/zlib) |
| // |
| |
| //go:build go1.16 |
| // +build go1.16 |
| |
| package main |
| |
| import ( |
| "flag" |
| "fmt" |
| "io/fs" |
| "log" |
| "net/http" |
| "os" |
| "runtime" |
| |
| "golang.org/x/website" |
| "golang.org/x/website/internal/godoc" |
| ) |
| |
| var ( |
| httpAddr = flag.String("http", "localhost:6060", "HTTP service address") |
| verbose = flag.Bool("v", false, "verbose mode") |
| goroot = flag.String("goroot", runtime.GOROOT(), "Go root directory") |
| templateDir = flag.String("templates", "", "load templates/JS/CSS from disk in this directory (usually /path-to-website/content)") |
| ) |
| |
| func usage() { |
| fmt.Fprintf(os.Stderr, "usage: golangorg\n") |
| flag.PrintDefaults() |
| os.Exit(2) |
| } |
| |
| func loggingHandler(h http.Handler) http.Handler { |
| return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| log.Printf("%s\t%s", req.RemoteAddr, req.URL) |
| h.ServeHTTP(w, req) |
| }) |
| } |
| |
| func main() { |
| earlySetup() |
| |
| flag.Usage = usage |
| flag.Parse() |
| |
| // Check usage. |
| if flag.NArg() > 0 { |
| fmt.Fprintln(os.Stderr, "Unexpected arguments.") |
| usage() |
| } |
| if *httpAddr == "" { |
| fmt.Fprintln(os.Stderr, "-http must be set") |
| usage() |
| } |
| |
| // Serve files from _content, falling back to GOROOT. |
| var content fs.FS |
| if *templateDir != "" { |
| content = os.DirFS(*templateDir) |
| } else { |
| content = website.Content |
| } |
| fsys = unionFS{content, os.DirFS(*goroot)} |
| |
| pres = godoc.NewPresentation(fsys) |
| pres.GoogleCN = googleCN |
| |
| readTemplates(pres) |
| mux := registerHandlers(pres) |
| lateSetup(mux) |
| |
| var handler http.Handler = http.DefaultServeMux |
| if *verbose { |
| log.Printf("golang.org server:") |
| log.Printf("\tversion = %s", runtime.Version()) |
| log.Printf("\taddress = %s", *httpAddr) |
| log.Printf("\tgoroot = %s", *goroot) |
| handler = loggingHandler(handler) |
| } |
| |
| // Start http server. |
| fmt.Fprintf(os.Stderr, "serving http://%s\n", *httpAddr) |
| if err := http.ListenAndServe(*httpAddr, handler); err != nil { |
| log.Fatalf("ListenAndServe %s: %v", *httpAddr, err) |
| } |
| } |
| |
| var _ fs.ReadDirFS = unionFS{} |
| |
| // A unionFS is an FS presenting the union of the file systems in the slice. |
| // If multiple file systems provide a particular file, Open uses the FS listed earlier in the slice. |
| // If multiple file systems provide a particular directory, ReadDir presents the |
| // concatenation of all the directories listed in the slice (with duplicates removed). |
| type unionFS []fs.FS |
| |
| func (fsys unionFS) Open(name string) (fs.File, error) { |
| var errOut error |
| for _, sub := range fsys { |
| f, err := sub.Open(name) |
| if err == nil { |
| // Note: Should technically check for directory |
| // and return a synthetic directory that merges |
| // reads from all the matching directories, |
| // but all the directory reads in internal/godoc |
| // come from fsys.ReadDir, which does that for us. |
| // So we can ignore direct f.ReadDir calls. |
| return f, nil |
| } |
| if errOut == nil { |
| errOut = err |
| } |
| } |
| return nil, errOut |
| } |
| |
| func (fsys unionFS) ReadDir(name string) ([]fs.DirEntry, error) { |
| var all []fs.DirEntry |
| var seen map[string]bool // seen[name] is true if name is listed in all; lazily initialized |
| var errOut error |
| for _, sub := range fsys { |
| list, err := fs.ReadDir(sub, toFS(name)) |
| if err != nil { |
| errOut = err |
| } |
| if len(all) == 0 { |
| all = append(all, list...) |
| } else { |
| if seen == nil { |
| // Initialize seen only after we get two different directory listings. |
| seen = make(map[string]bool) |
| for _, d := range all { |
| seen[d.Name()] = true |
| } |
| } |
| for _, d := range list { |
| name := d.Name() |
| if !seen[name] { |
| seen[name] = true |
| all = append(all, d) |
| } |
| } |
| } |
| } |
| if len(all) > 0 { |
| return all, nil |
| } |
| return nil, errOut |
| } |