diff options
| author | Jonathan Amsterdam <jba@google.com> | 2021-10-27 08:36:05 -0400 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2021-10-28 17:01:30 +0000 |
| commit | 818e48fe610d0d0c7166ffbf5ee29e157f40206a (patch) | |
| tree | 6498a0c6fa572d8cb8382687e09e9e97e0304bc4 /cmd/pkgsite | |
| parent | 8256ffbafbbe31d9bae07cf47ef88ff4b137f5b1 (diff) | |
| download | go-x-pkgsite-818e48fe610d0d0c7166ffbf5ee29e157f40206a.tar.xz | |
many: cmd/pkgsite embeds static assets
The `pkgsite` command now embeds all the static assets it needs, so it
need not be told the location of the static/ directory with a
flag. The binary is self-contained and can be placed and invoked from
anywhere.
This required embedding the static/ and third_party/
directories. Since //go:embed cannot reference files outside the
containing package's tree, we had to add trivial Go packages in
static/ and third_party/ to construct `embed.FS`s for those
directories.
Also, the frontend needed to accept `fs.FS` values where it
previously took paths, and `template.TrustedFS` values where it
previously used `template.TrustedSources`. We ended up clumsily
requiring four separate config values:
- A TrustedFS to load templates.
- Two fs.FSs, one for static and one for third_party, to load other
assets.
- The path to the static directory as a string, solely to support
dynamic loading in dev mode.
For golang/go#48410
Change-Id: I9eeb351b1c6f23444b9e65b60f2a1d3905d59ef9
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/359395
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
Diffstat (limited to 'cmd/pkgsite')
| -rw-r--r-- | cmd/pkgsite/main.go | 33 | ||||
| -rw-r--r-- | cmd/pkgsite/main_test.go | 2 |
2 files changed, 20 insertions, 15 deletions
diff --git a/cmd/pkgsite/main.go b/cmd/pkgsite/main.go index 31e55c8e..d54a9dcf 100644 --- a/cmd/pkgsite/main.go +++ b/cmd/pkgsite/main.go @@ -6,29 +6,28 @@ // It runs as a web server and presents the documentation as a // web page. // -// After running `go install ./cmd/pkgsite` from the pkgsite repo root, you can -// run `pkgsite` from anywhere, but if you don't run it from the pkgsite repo -// root you must specify the location of the static assets with -static. +// To install, run `go install ./cmd/pkgsite` from the pkgsite repo root. // -// With just -static, pkgsite will serve docs for the module in the current +// With no arguments, pkgsite will serve docs for the module in the current // directory, which must have a go.mod file: // -// cd ~/repos/cue && pkgsite -static ~/repos/pkgsite/static +// cd ~/repos/cue && pkgsite // // You can also serve docs from your module cache, directly from the proxy // (it uses the GOPROXY environment variable), or both: // -// pkgsite -static ~/repos/pkgsite/static -cache -proxy +// pkgsite -cache -proxy // -// With either -cache or -proxy, it won't look for a module in the current directory. -// You can still provide modules on the local filesystem by listing their paths: +// 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 -static ~/repos/pkgsite/static -cache -proxy ~/repos/cue some/other/module +// 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), -// provide its location with the -gorepo flag to save a little time. +// you can provide its location with the -gorepo flag to save a little time. package main import ( @@ -51,12 +50,14 @@ import ( "golang.org/x/pkgsite/internal/proxy" "golang.org/x/pkgsite/internal/source" "golang.org/x/pkgsite/internal/stdlib" + "golang.org/x/pkgsite/static" + thirdparty "golang.org/x/pkgsite/third_party" ) const defaultAddr = "localhost:8080" // default webserver address var ( - _ = flag.String("static", "static", "path to folder containing static files served") + staticFlag = flag.String("static", "", "OBSOLETE - DO NOT USE") gopathMode = flag.Bool("gopath_mode", false, "assume that local modules' paths are relative to GOPATH/src") httpAddr = flag.String("http", defaultAddr, "HTTP service address to listen for incoming requests on") useCache = flag.Bool("cache", false, "fetch from the module cache") @@ -76,6 +77,10 @@ func main() { flag.Parse() ctx := context.Background() + if *staticFlag != "" { + fmt.Fprintf(os.Stderr, "-static is ignored. It is obsolete and may be removed in a future version.\n") + } + paths := collectPaths(flag.Args()) if len(paths) == 0 && !*useCache && !*useProxy { paths = []string{"."} @@ -118,7 +123,7 @@ func main() { server, err := newServer(ctx, paths, *gopathMode, modCacheDir, prox) if err != nil { - die("%s\nMaybe you need to provide the location of static assets with -static.", err) + die("%s", err) } router := http.NewServeMux() server.Install(router.Handle, nil, nil) @@ -160,7 +165,9 @@ func newServer(ctx context.Context, paths []string, gopathMode bool, downloadDir }.New() server, err := frontend.NewServer(frontend.ServerConfig{ DataSourceGetter: func(context.Context) internal.DataSource { return lds }, - StaticPath: template.TrustedSourceFromFlag(flag.Lookup("static").Value), + TemplateFS: template.TrustedFSFromEmbed(static.FS), + StaticFS: static.FS, + ThirdPartyFS: thirdparty.FS, }) if err != nil { return nil, err diff --git a/cmd/pkgsite/main_test.go b/cmd/pkgsite/main_test.go index a938eb3c..9a84ee4a 100644 --- a/cmd/pkgsite/main_test.go +++ b/cmd/pkgsite/main_test.go @@ -6,7 +6,6 @@ package main import ( "context" - "flag" "net/http" "net/http/httptest" "os" @@ -45,7 +44,6 @@ func Test(t *testing.T) { localModule := repoPath("internal/fetch/testdata/has_go_mod") cacheDir := repoPath("internal/fetch/testdata/modcache") - flag.Set("static", repoPath("static")) testModules := proxytest.LoadTestModules(repoPath("internal/proxy/testdata")) prox, teardown := proxytest.SetupTestClient(t, testModules) defer teardown() |
