refactor: instantiate AssetCollection explicitly
4 files changed, 46 insertions(+), 35 deletions(-)
M cmd/searchix-web/main.go → cmd/searchix-web/main.go
@@ -55,14 +55,19 @@ } os.Exit(0) } if *generateErrorPage { - err := components.ErrorTemplate(components.TemplateData{ + assets, err := frontend.New() + if err != nil { + panic("failed to create assets: " + err.Error()) + } + + err = components.ErrorTemplate(components.TemplateData{ Source: nil, Sources: []*config.Source{}, Query: "", ExtraHeadHTML: "", Code: 0, Message: `{{placeholder "http.error.status_code"}} {{placeholder "http.error.status_text"}}`, - Assets: frontend.Assets, + Assets: assets, }).Render(os.Stdout) if err != nil { panic("failed to render error template: " + err.Error())
M frontend/assets.go → frontend/assets.go
@@ -12,12 +12,6 @@ "github.com/Southclaws/fault" "github.com/Southclaws/fault/fmsg" ) -var Assets = &AssetCollection{ - Scripts: []*Asset{}, - Stylesheets: []*Asset{}, - ByPath: make(map[string]*Asset), -} - type Asset struct { URL string ETag string@@ -31,6 +25,21 @@ Stylesheets []*Asset ByPath map[string]*Asset } +func New() (*AssetCollection, error) { + a := &AssetCollection{ + Scripts: []*Asset{}, + Stylesheets: []*Asset{}, + ByPath: make(map[string]*Asset), + } + + err := a.Rehash() + if err != nil { + return nil, err + } + + return a, nil +} + func newAsset(filename string) (*Asset, error) { file, err := Files.Open(filename) if err != nil {@@ -52,7 +61,7 @@ Base64SHA256: base64.StdEncoding.EncodeToString(shasum.Sum(nil)), }, nil } -func hashScripts() error { +func (a *AssetCollection) hashScripts() error { scripts, err := fs.Glob(Files, "static/**.js") if err != nil { return fault.Wrap(err, fmsg.With("could not glob files"))@@ -62,14 +71,14 @@ asset, err := newAsset(filename) if err != nil { return err } - Assets.Scripts = append(Assets.Scripts, asset) - Assets.ByPath[asset.URL] = asset + a.Scripts = append(a.Scripts, asset) + a.ByPath[asset.URL] = asset } return nil } -func hashStyles() error { +func (a *AssetCollection) hashStyles() error { styles, err := fs.Glob(Files, "static/**.css") if err != nil { return fault.Wrap(err, fmsg.With("could not glob files"))@@ -79,31 +88,24 @@ asset, err := newAsset(filename) if err != nil { return err } - Assets.Stylesheets = append(Assets.Stylesheets, asset) - Assets.ByPath[asset.URL] = asset + a.Stylesheets = append(a.Stylesheets, asset) + a.ByPath[asset.URL] = asset } return nil } -func Rehash() (err error) { - Assets.Scripts = []*Asset{} - err = hashScripts() +func (a *AssetCollection) Rehash() (err error) { + a.Scripts = []*Asset{} + err = a.hashScripts() if err != nil { return err } - Assets.Stylesheets = []*Asset{} - err = hashStyles() + a.Stylesheets = []*Asset{} + err = a.hashStyles() if err != nil { return err } return nil } - -func init() { - err := Rehash() - if err != nil { - panic(err) - } -}
M internal/server/error.go → internal/server/error.go
@@ -11,6 +11,7 @@ ) func createErrorHandler( config *config.Config, + assets *frontend.AssetCollection, log *log.Logger, ) func(http.ResponseWriter, *http.Request, string, int) { return func(w http.ResponseWriter, r *http.Request, message string, code int) {@@ -23,7 +24,7 @@ ExtraHeadHTML: config.Web.ExtraHeadHTML, Sources: sources, Code: code, Message: message, - Assets: frontend.Assets, + Assets: assets, } w.Header().Set("Cache-Control", "no-store") w.Header().Del("Vary")
M internal/server/mux.go → internal/server/mux.go
@@ -71,8 +71,12 @@ return nil, fault.New("read index is nil") } index := options.ReadIndex sortSources(cfg.Importer.Sources) + assets, err := frontend.New() + if err != nil { + return nil, fault.Wrap(err, fmsg.With("could not create frontend asset collection")) + } - errorHandler := createErrorHandler(cfg, log) + errorHandler := createErrorHandler(cfg, assets, log) top := http.NewServeMux() mux := sentryhttp.NewServeMux()@@ -143,7 +147,7 @@ TemplateData: components.TemplateData{ ExtraHeadHTML: cfg.Web.ExtraHeadHTML, Source: source, Sources: sources, - Assets: frontend.Assets, + Assets: assets, Query: qs, }, Query: qs,@@ -197,7 +201,7 @@ components.TemplateData{ ExtraHeadHTML: cfg.Web.ExtraHeadHTML, Sources: sources, Source: source, - Assets: frontend.Assets, + Assets: assets, }, components.ResultData{}, ).Render(w)@@ -251,7 +255,7 @@ tdata := components.TemplateData{ ExtraHeadHTML: cfg.Web.ExtraHeadHTML, Source: source, Sources: sources, - Assets: frontend.Assets, + Assets: assets, } var baseErr error if r.Header.Get("Fetch") == "true" {@@ -347,7 +351,7 @@ } }) mux.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) { - asset, found := frontend.Assets.ByPath[r.URL.Path] + asset, found := assets.ByPath[r.URL.Path] if !found { http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)@@ -360,8 +364,7 @@ http.ServeFileFS(w, r, frontend.Files, asset.Filename) }) mdb := options.ManpagesURLMap - err := mdb.Open() - if err != nil { + if err := mdb.Open(); err != nil { return nil, fault.Wrap(err, fmsg.With("failed to open manpages URL map")) } mux.HandleFunc("/man/{section}/{page}", func(w http.ResponseWriter, r *http.Request) {@@ -395,7 +398,7 @@ } go fw.Start(func(filename string) { log.Debug(fmt.Sprintf("got filename %s", filename)) if match, _ := path.Match("frontend/static/*", filename); match { - err := frontend.Rehash() + err := assets.Rehash() if err != nil { log.Error("failed to re-hash frontend assets", "error", err) }