aboutsummaryrefslogtreecommitdiff
path: root/cmd/pkgsite
diff options
context:
space:
mode:
authorJulie Qiu <julie@golang.org>2021-02-05 16:16:41 -0500
committerJulie Qiu <julie@golang.org>2021-02-05 22:02:34 +0000
commitb3177424ae94ce090ee582162f87e455b612edff (patch)
treee11d2d8d5544e3e1dfd5d07d9296f6d72e663a21 /cmd/pkgsite
parente10f26fe46a5a4b21981fd6aa138760a3b248b90 (diff)
downloadgo-x-pkgsite-b3177424ae94ce090ee582162f87e455b612edff.tar.xz
cmd/pkgsite: add command
Functionality for running the pkgsite frontend locally is moved from cmd/frontend to cmd/pkgsite, since cmd/frontend is currently overloaded with flag options and running locally does not need all the dependencies for running cmd/frontend. Additional functionality will be added to cmd/pkgsite in future CLs. For golang/go#40371 Change-Id: I4230aa9539c94e01a68eda33cc6492ae377debff Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/290134 Reviewed-by: Jamal Carvalho <jamal@golang.org> Trust: Julie Qiu <julie@golang.org>
Diffstat (limited to 'cmd/pkgsite')
-rw-r--r--cmd/pkgsite/main.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/cmd/pkgsite/main.go b/cmd/pkgsite/main.go
new file mode 100644
index 00000000..bfb1b0a3
--- /dev/null
+++ b/cmd/pkgsite/main.go
@@ -0,0 +1,99 @@
+// 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.
+
+// This is a work in progress.
+//
+// Pkgsite extracts and generates documentation for Go programs.
+// It runs as a web server and presents the documentation as a
+// web page.
+// Usage:
+//
+// pkgsite [flag]
+//
+// The flags are:
+//
+// -local=path1,path2
+// Accepts a GOPATH-like collection of local paths for modules to load to memory
+// -gopath_mode=false
+// Assume that local modules' paths are relative to GOPATH/src
+package main
+
+import (
+ "context"
+ "flag"
+ "net/http"
+ "path/filepath"
+ "time"
+
+ "github.com/google/safehtml/template"
+ "golang.org/x/pkgsite/internal"
+ "golang.org/x/pkgsite/internal/dcensus"
+ "golang.org/x/pkgsite/internal/frontend"
+ "golang.org/x/pkgsite/internal/localdatasource"
+ "golang.org/x/pkgsite/internal/log"
+ "golang.org/x/pkgsite/internal/middleware"
+)
+
+var (
+ _ = flag.String("static", "content/static", "path to folder containing static files served")
+ localPaths = flag.String("local", "", "run locally, accepts a GOPATH-like collection of local paths for modules to load to memory")
+ gopathMode = flag.Bool("gopath_mode", false, "assume that local modules' paths are relative to GOPATH/src, used only with -local")
+)
+
+func main() {
+ flag.Parse()
+ ctx := context.Background()
+ var dsg func(context.Context) internal.DataSource
+ if *localPaths == "" {
+ log.Fatalf(ctx, "-local is not set")
+ }
+
+ lds := localdatasource.New()
+ dsg = func(context.Context) internal.DataSource { return lds }
+ server, err := frontend.NewServer(frontend.ServerConfig{
+ DataSourceGetter: dsg,
+ StaticPath: template.TrustedSourceFromFlag(flag.Lookup("static").Value),
+ })
+ if err != nil {
+ log.Fatalf(ctx, "frontend.NewServer: %v", err)
+ }
+ lds, ok := dsg(ctx).(*localdatasource.DataSource)
+ if ok {
+ load(ctx, lds, *localPaths)
+ }
+
+ router := dcensus.NewRouter(frontend.TagRoute)
+ server.Install(router.Handle, nil, nil)
+
+ mw := middleware.Chain(
+ middleware.RedirectedFrom(),
+ middleware.LatestVersions(server.GetLatestInfo), // must come before caching for version badge to work
+ middleware.Timeout(54*time.Second),
+ )
+ addr := "localhost:6060"
+ log.Infof(ctx, "Listening on addr %s", addr)
+ log.Fatal(ctx, http.ListenAndServe(addr, mw(router)))
+}
+
+// load loads local modules from pathList.
+func load(ctx context.Context, ds *localdatasource.DataSource, pathList string) {
+ paths := filepath.SplitList(pathList)
+ loaded := len(paths)
+ for _, path := range paths {
+ var err error
+ if *gopathMode {
+ err = ds.LoadFromGOPATH(ctx, path)
+ } else {
+ err = ds.Load(ctx, path)
+ }
+ if err != nil {
+ log.Error(ctx, err)
+ loaded--
+ }
+ }
+
+ if loaded == 0 {
+ log.Fatalf(ctx, "failed to load module(s) at %s", pathList)
+ }
+}