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...
This commit is contained in:
parent
25443cfaab
commit
5846385513
3 changed files with 14 additions and 12 deletions
|
@ -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
|
||||
|
|
|
@ -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 {
|
|
@ -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
|
Loading…
Reference in a new issue