From 5846385513232f6e71dbe2a022a9c64398011a42 Mon Sep 17 00:00:00 2001 From: Luke Granger-Brown Date: Sat, 20 Mar 2021 23:43:59 +0000 Subject: [PATCH] fup: move hashfs into its own package. We need to depend on its API for fuphttp, so it's better if it's a separate package to avoid embedding things we don't need. In general it's probably a good idea to separate the logic from the embedded content... --- web/fup/fupstatic/fupstatic.go | 4 +++- web/fup/{fupstatic => hashfs}/hashfs.go | 18 +++++++++--------- web/fup/{fupstatic => hashfs}/hashfs_test.go | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) rename web/fup/{fupstatic => hashfs}/hashfs.go (90%) rename web/fup/{fupstatic => hashfs}/hashfs_test.go (95%) diff --git a/web/fup/fupstatic/fupstatic.go b/web/fup/fupstatic/fupstatic.go index 1ad4bceb80..e9442716ae 100644 --- a/web/fup/fupstatic/fupstatic.go +++ b/web/fup/fupstatic/fupstatic.go @@ -7,12 +7,14 @@ package fupstatic import ( "embed" "io/fs" + + "hg.lukegb.com/lukegb/depot/web/fup/hashfs" ) //go:embed css js var static embed.FS -var Static fs.FS = newStaticFS(static) +var Static *hashfs.FS = hashfs.New(static) //go:embed tmpl var templates embed.FS diff --git a/web/fup/fupstatic/hashfs.go b/web/fup/hashfs/hashfs.go similarity index 90% rename from web/fup/fupstatic/hashfs.go rename to web/fup/hashfs/hashfs.go index ede0c3a49b..2eafa5d126 100644 --- a/web/fup/fupstatic/hashfs.go +++ b/web/fup/hashfs/hashfs.go @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 -package fupstatic +package hashfs import ( "crypto/sha512" @@ -23,7 +23,7 @@ func adaptError(err error, name string) error { return err } -type staticFS struct { +type FS struct { fs fs.ReadFileFS // We need: @@ -70,7 +70,7 @@ func (f *staticFSFile) Stat() (fs.FileInfo, error) { type staticFSDirFile struct { fs.ReadDirFile basePath string - s *staticFS + s *FS } func (f staticFSDirFile) ReadDir(n int) ([]fs.DirEntry, error) { @@ -88,7 +88,7 @@ func (f staticFSDirFile) ReadDir(n int) ([]fs.DirEntry, error) { } // build computes the hashes needed to serve files. -func (s *staticFS) build() error { +func (s *FS) build() error { toHashName := make(map[string]string) toPlainName := make(map[string]string) if err := fs.WalkDir(s.fs, ".", func(fpath string, d fs.DirEntry, err error) error { @@ -129,13 +129,13 @@ func (s *staticFS) build() error { } // LookupHashedName looks up the filename for the given original name. -func (s *staticFS) LookupHashedName(name string) (string, bool) { +func (s *FS) LookupHashedName(name string) (string, bool) { n, ok := s.toHashName[name] return n, ok } // Open opens the named file. -func (s *staticFS) Open(name string) (fs.File, error) { +func (s *FS) Open(name string) (fs.File, error) { fn, ok := s.toPlainName[name] if !ok { // Try opening it as a plain name. @@ -152,7 +152,7 @@ func (s *staticFS) Open(name string) (fs.File, error) { } // ReadFile provides an optimised ReadFile implementation. -func (s *staticFS) ReadFile(name string) ([]byte, error) { +func (s *FS) ReadFile(name string) ([]byte, error) { fn, ok := s.toPlainName[name] if !ok { // Try opening it as a plain name. @@ -165,8 +165,8 @@ func (s *staticFS) ReadFile(name string) ([]byte, error) { return f, nil } -func newStaticFS(fs fs.ReadFileFS) *staticFS { - s := &staticFS{ +func New(fs fs.ReadFileFS) *FS { + s := &FS{ fs: fs, } if err := s.build(); err != nil { diff --git a/web/fup/fupstatic/hashfs_test.go b/web/fup/hashfs/hashfs_test.go similarity index 95% rename from web/fup/fupstatic/hashfs_test.go rename to web/fup/hashfs/hashfs_test.go index c60923f112..7374824627 100644 --- a/web/fup/fupstatic/hashfs_test.go +++ b/web/fup/hashfs/hashfs_test.go @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 -package fupstatic +package hashfs import ( "testing" @@ -19,7 +19,7 @@ func TestHashingFS(t *testing.T) { }, } - f := newStaticFS(baseFS) + f := New(baseFS) tcs := []struct { origName string