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 (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
|
||||||
|
"hg.lukegb.com/lukegb/depot/web/fup/hashfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed css js
|
//go:embed css js
|
||||||
var static embed.FS
|
var static embed.FS
|
||||||
|
|
||||||
var Static fs.FS = newStaticFS(static)
|
var Static *hashfs.FS = hashfs.New(static)
|
||||||
|
|
||||||
//go:embed tmpl
|
//go:embed tmpl
|
||||||
var templates embed.FS
|
var templates embed.FS
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package fupstatic
|
package hashfs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
|
@ -23,7 +23,7 @@ func adaptError(err error, name string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type staticFS struct {
|
type FS struct {
|
||||||
fs fs.ReadFileFS
|
fs fs.ReadFileFS
|
||||||
|
|
||||||
// We need:
|
// We need:
|
||||||
|
@ -70,7 +70,7 @@ func (f *staticFSFile) Stat() (fs.FileInfo, error) {
|
||||||
type staticFSDirFile struct {
|
type staticFSDirFile struct {
|
||||||
fs.ReadDirFile
|
fs.ReadDirFile
|
||||||
basePath string
|
basePath string
|
||||||
s *staticFS
|
s *FS
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f staticFSDirFile) ReadDir(n int) ([]fs.DirEntry, error) {
|
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.
|
// build computes the hashes needed to serve files.
|
||||||
func (s *staticFS) build() error {
|
func (s *FS) build() error {
|
||||||
toHashName := make(map[string]string)
|
toHashName := make(map[string]string)
|
||||||
toPlainName := make(map[string]string)
|
toPlainName := make(map[string]string)
|
||||||
if err := fs.WalkDir(s.fs, ".", func(fpath string, d fs.DirEntry, err error) error {
|
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.
|
// 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]
|
n, ok := s.toHashName[name]
|
||||||
return n, ok
|
return n, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open opens the named file.
|
// 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]
|
fn, ok := s.toPlainName[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
// Try opening it as a plain name.
|
// 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.
|
// 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]
|
fn, ok := s.toPlainName[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
// Try opening it as a plain name.
|
// Try opening it as a plain name.
|
||||||
|
@ -165,8 +165,8 @@ func (s *staticFS) ReadFile(name string) ([]byte, error) {
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newStaticFS(fs fs.ReadFileFS) *staticFS {
|
func New(fs fs.ReadFileFS) *FS {
|
||||||
s := &staticFS{
|
s := &FS{
|
||||||
fs: fs,
|
fs: fs,
|
||||||
}
|
}
|
||||||
if err := s.build(); err != nil {
|
if err := s.build(); err != nil {
|
|
@ -2,7 +2,7 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package fupstatic
|
package hashfs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -19,7 +19,7 @@ func TestHashingFS(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
f := newStaticFS(baseFS)
|
f := New(baseFS)
|
||||||
|
|
||||||
tcs := []struct {
|
tcs := []struct {
|
||||||
origName string
|
origName string
|
Loading…
Reference in a new issue