Skip to content

Commit 0ddffae

Browse files
committed
Simple "server" implementation for catalog.
Generates the html and serves. It requires a different url-prefix for links so beware of that. We also need to think about how to handle mirrors and download links more effectively.
1 parent 8751fe7 commit 0ddffae

2 files changed

Lines changed: 102 additions & 20 deletions

File tree

app/_docs/warpforge-catalog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Generates HTML output for the root workspace catalog containing information on m
4949
### mirror
5050
Mirror the contents of a catalog to remote warehouses
5151
52+
### serve
53+
Generates html and serves
54+
5255
### help, h
5356
Shows a list of commands or help for one command
5457

app/catalog/catalog_cli.go

Lines changed: 99 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"net/http"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -156,6 +157,37 @@ var catalogCmdDef = &cli.Command{
156157
util.CmdMiddlewareTracingSpan,
157158
),
158159
},
160+
{
161+
Name: "serve",
162+
Usage: "Generates html and serves",
163+
Action: util.ChainCmdMiddleware(cmdServe,
164+
util.CmdMiddlewareLogging,
165+
util.CmdMiddlewareTracingConfig,
166+
util.CmdMiddlewareTracingSpan,
167+
),
168+
Flags: []cli.Flag{
169+
&cli.StringFlag{
170+
Name: "output",
171+
Aliases: []string{"o"},
172+
Usage: "Output path for HTML generation",
173+
},
174+
&cli.StringFlag{
175+
Name: "url-prefix",
176+
Usage: "URL prefix for links within generated HTML",
177+
Value: "/",
178+
},
179+
&cli.StringFlag{
180+
Name: "download-url",
181+
Usage: "URL for warehouse to use for download links",
182+
},
183+
&cli.UintFlag{
184+
Name: "port",
185+
Aliases: []string{"p"},
186+
Usage: "Port number for the server address",
187+
Value: 8080,
188+
},
189+
},
190+
},
159191
},
160192
}
161193

@@ -715,69 +747,116 @@ func cmdCatalogShow(c *cli.Context) error {
715747
return nil
716748
}
717749

718-
func cmdGenerateHtml(c *cli.Context) error {
719-
catalogName := c.String("name")
750+
type protoSiteConfig struct {
751+
catalogName string
752+
outputPathOverride string
753+
urlPrefix string
754+
downloadUrl string
755+
}
720756

757+
func (cfg protoSiteConfig) SiteConfig(ctx context.Context) (cataloghtml.SiteConfig, error) {
721758
// open the workspace set
722759
wsSet, err := util.OpenWorkspaceSet()
723760
if err != nil {
724-
return err
761+
return cataloghtml.SiteConfig{}, err
725762
}
726763

727-
// create the catalog if it does not exist
728-
exists, err := wsSet.Root().HasCatalog(catalogName)
764+
exists, err := wsSet.Root().HasCatalog(cfg.catalogName)
729765
if err != nil {
730-
return err
766+
return cataloghtml.SiteConfig{}, err
731767
}
732768
if !exists {
733-
return fmt.Errorf("catalog %q not found", catalogName)
769+
return cataloghtml.SiteConfig{}, fmt.Errorf("catalog %q not found", cfg.catalogName)
734770
}
735771

736-
cat, err := wsSet.Root().OpenCatalog(catalogName)
772+
cat, err := wsSet.Root().OpenCatalog(cfg.catalogName)
737773
if err != nil {
738-
return fmt.Errorf("failed to open catalog %q: %s", catalogName, err)
774+
return cataloghtml.SiteConfig{}, fmt.Errorf("failed to open catalog %q: %s", cfg.catalogName, err)
739775
}
740776

741777
// by default, output to a subdir of the catalog named `_html`
742778
// this can be overriden by a cli flag that provides a path
743-
outputPath, err := wsSet.Root().CatalogPath(catalogName)
779+
outputPath, err := wsSet.Root().CatalogPath(cfg.catalogName)
744780
if err != nil {
745-
return err
781+
return cataloghtml.SiteConfig{}, err
746782
}
747783
outputPath = filepath.Join("/", outputPath, "_html")
748-
if c.String("output") != "" {
749-
outputPath = c.String("output")
784+
if cfg.outputPathOverride != "" {
785+
outputPath = cfg.outputPathOverride
750786
}
751787

752788
// by default, the URL prefix is the same as the output path,
753789
// this works if the HTML is accessed using `file:///` URLs.
754790
// however, to allow for generating a hosted site, this can be
755791
// overridden by the CLI
756792
urlPrefix := outputPath
757-
if c.String("url-prefix") != "" {
758-
urlPrefix = c.String("url-prefix")
793+
if cfg.urlPrefix != "" {
794+
urlPrefix = cfg.urlPrefix
759795
}
760796

761797
var warehouseUrl *string = nil
762-
if c.String("download-url") != "" {
763-
dlUrl := c.String("download-url")
798+
if cfg.downloadUrl != "" {
799+
dlUrl := cfg.downloadUrl
764800
warehouseUrl = &dlUrl
765801
}
766802

767-
cfg := cataloghtml.SiteConfig{
768-
Ctx: context.Background(),
803+
return cataloghtml.SiteConfig{
804+
Ctx: ctx,
769805
Cat_dab: cat,
770806
OutputPath: outputPath,
771807
URLPrefix: urlPrefix,
772808
DownloadURL: warehouseUrl,
809+
}, nil
810+
}
811+
812+
func cmdGenerateHtml(c *cli.Context) error {
813+
pcfg := protoSiteConfig{
814+
catalogName: c.String("name"),
815+
outputPathOverride: c.String("output"),
816+
urlPrefix: c.String("url-prefix"),
817+
downloadUrl: c.String("download-url"),
773818
}
819+
cfg, err := pcfg.SiteConfig(c.Context)
820+
if err != nil {
821+
return err
822+
}
823+
774824
os.RemoveAll(cfg.OutputPath)
775825
if err := cfg.CatalogAndChildrenToHtml(); err != nil {
776826
return fmt.Errorf("failed to generate html: %s", err)
777827
}
778828

779-
fmt.Printf("published HTML for catalog %q to %s\n", catalogName, outputPath)
829+
fmt.Printf("published HTML for catalog %q to %s\n", pcfg.catalogName, cfg.OutputPath)
830+
831+
return nil
832+
}
780833

834+
func cmdServe(c *cli.Context) error {
835+
pcfg := protoSiteConfig{
836+
catalogName: c.String("name"),
837+
outputPathOverride: c.String("output"),
838+
urlPrefix: c.String("url-prefix"),
839+
downloadUrl: c.String("download-url"),
840+
}
841+
port := c.Uint("port")
842+
addr := fmt.Sprintf("%s:%d", "localhost", port)
843+
844+
cfg, err := pcfg.SiteConfig(c.Context)
845+
if err != nil {
846+
return err
847+
}
848+
os.RemoveAll(cfg.OutputPath)
849+
if err := cfg.CatalogAndChildrenToHtml(); err != nil {
850+
return fmt.Errorf("failed to generate html: %s", err)
851+
}
852+
853+
fmt.Printf("published HTML for catalog %q to %s\n", pcfg.catalogName, cfg.OutputPath)
854+
855+
dirHandler := http.Dir(cfg.OutputPath)
856+
fmt.Printf("Serving at http://%s\n", addr)
857+
if err := http.ListenAndServe(addr, http.FileServer(dirHandler)); err != nil {
858+
return err
859+
}
781860
return nil
782861
}
783862

0 commit comments

Comments
 (0)