all repos — searchix @ dec2c516100350a78f0b7116bc6f9d76325e7760

Search engine for NixOS, nix-darwin, home-manager and NUR users

refactor: instantiate AssetCollection explicitly

Alan Pearce
commit

dec2c516100350a78f0b7116bc6f9d76325e7760

parent

0ae74e5f6af61819e8f954960287ea14ec447834

1 file changed, 26 insertions(+), 24 deletions(-)

changed files
M frontend/assets.gofrontend/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) - } -}