Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
170 lines (150 loc) · 5.26 KB
/
Copy pathmain.go
File metadata and controls
170 lines (150 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2021 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.
// Pkgsite extracts and generates documentation for Go programs.
// It runs as a web server and presents the documentation as a
// web page.
//
// To install, run:
//
// go install golang.org/x/pkgsite/cmd/pkgsite@latest
//
// With no arguments, pkgsite will serve docs for main modules relative to the
// current directory, i.e. the modules listed by `go list -m`. This is
// typically the module defined by the nearest go.mod file in a parent
// directory. However, this may include multiple main modules when using a
// go.work file to define a [workspace].
//
// For example, both of the following forms could be used to work
// on the module defined in repos/cue/go.mod:
//
// The single module form:
//
// cd repos/cue && pkgsite
//
// The multiple module form:
//
// go work init repos/cue repos/other && pkgsite
//
// By default, the resulting server will also serve all of the module's
// dependencies at their required versions. You can disable serving the
// required modules by passing -list=false.
//
// You can also serve docs from your module cache, directly from the proxy
// (it uses the GOPROXY environment variable), or both:
//
// pkgsite -cache -proxy
//
// With either -cache or -proxy, pkgsite won't look for a module in the current
// directory. You can still provide modules on the local filesystem by listing
// their paths:
//
// pkgsite -cache -proxy ~/repos/cue some/other/module
//
// Although standard library packages will work by default, the docs can take a
// while to appear the first time because the Go repo must be cloned and
// processed. If you clone the repo yourself (https://go.googlesource.com/go),
// you can provide its location with the -gorepo flag to save a little time.
//
// [workspace]: https://go.dev/ref/mod#workspaces
package main
import (
"context"
"flag"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
"golang.org/x/pkgsite/cmd/internal/pkgsite"
"golang.org/x/pkgsite/internal/browser"
"golang.org/x/pkgsite/internal/log"
"golang.org/x/pkgsite/internal/middleware/timeout"
"golang.org/x/pkgsite/internal/proxy"
"golang.org/x/pkgsite/internal/stdlib"
)
const defaultAddr = "localhost:8080" // default webserver address
var (
httpAddr = flag.String("http", defaultAddr, "HTTP service address to listen for incoming requests on")
goRepoPath = flag.String("gorepo", "", "path to Go repo on local filesystem")
useProxy = flag.Bool("proxy", false, "fetch from GOPROXY if not found locally")
openFlag = flag.Bool("open", false, "open a browser window to the server's address")
// other flags are bound to ServerConfig below
)
func main() {
var serverCfg pkgsite.ServerConfig
flag.BoolVar(&serverCfg.GOPATHMode, "gopath_mode", false, "assume that local modules' Paths are relative to GOPATH/src")
flag.BoolVar(&serverCfg.UseCache, "cache", false, "fetch from the module cache")
flag.StringVar(&serverCfg.CacheDir, "cachedir", "", "module cache directory (defaults to `go env GOMODCACHE`)")
flag.BoolVar(&serverCfg.UseListedMods, "list", true, "for each path, serve all modules in build list")
flag.BoolVar(&serverCfg.DevMode, "dev", false, "enable developer mode (reload templates on each page load, serve non-minified JS/CSS, etc.)")
flag.StringVar(&serverCfg.DevModeStaticDir, "static", "static", "path to folder containing static files served")
flag.Usage = func() {
out := flag.CommandLine.Output()
fmt.Fprintf(out, "usage: %s [flags] [PATHS ...]\n", os.Args[0])
fmt.Fprintf(out, " where each PATHS is a single path or a comma-separated list\n")
fmt.Fprintf(out, " (default is current directory if neither -cache nor -proxy is provided)\n")
flag.PrintDefaults()
}
flag.Parse()
serverCfg.UseLocalStdlib = true
serverCfg.GoRepoPath = *goRepoPath
serverCfg.Paths = collectPaths(flag.Args())
if serverCfg.UseCache || *useProxy {
fmt.Fprintf(os.Stderr, "BYPASSING LICENSE CHECKING: MAY DISPLAY NON-REDISTRIBUTABLE INFORMATION\n")
}
if *useProxy {
url := os.Getenv("GOPROXY")
if url == "" {
dief("GOPROXY environment variable is not set")
}
var err error
serverCfg.Proxy, err = proxy.New(url, nil)
if err != nil {
dief("connecting to proxy: %s", err)
}
}
if *goRepoPath != "" {
stdlib.SetGoRepoPath(*goRepoPath)
}
ctx := context.Background()
server, err := pkgsite.BuildServer(ctx, serverCfg)
if err != nil {
dief("%s", err)
}
addr := *httpAddr
if addr == "" {
addr = ":http"
}
ln, err := net.Listen("tcp", addr)
if err != nil {
dief("%s", err)
}
url := "http://" + addr
log.Infof(ctx, "Listening on addr %s", url)
if *openFlag {
go func() {
if !browser.Open(url) {
log.Infof(ctx, "Failed to open browser window. Please visit %s in your browser.", url)
}
}()
}
router := http.NewServeMux()
server.Install(router.Handle, nil, nil)
mw := timeout.Timeout(54 * time.Second)
srv := &http.Server{Addr: addr, Handler: mw(router)}
dief("%v", srv.Serve(ln))
}
func dief(format string, args ...any) {
fmt.Fprintf(os.Stderr, format, args...)
fmt.Fprintln(os.Stderr)
os.Exit(1)
}
func collectPaths(args []string) []string {
var paths []string
for _, arg := range args {
paths = append(paths, strings.Split(arg, ",")...)
}
return paths
}