mycorrhiza/static/static.go
handlerug 839f8bc2d6
New static files system
assets.qtpl is no more! Now you can just add files to static/ folder,
make sure the extension is registered in static.go (a shortcoming
that'll be addressed in future Go versions), and you're done! It
searches the local file system first, then falls back to the files
embedded with the binary (in the static/ folder).
2021-06-12 20:58:04 +07:00

51 lines
959 B
Go

package static
import (
"embed"
"io/fs"
"log"
"os"
)
//go:embed *.css *.js icon
var embedFS embed.FS
// FS serves all static files.
var FS HybridFS
// HybridFS is a filesystem that implements fs.FS. It can serve files
// from multiple filesystems, falling back on failures.
type HybridFS struct {
fs []fs.FS
}
// Open tries to open the requested file using all filesystems provided.
// If neither succeeds, it returns the last error.
func (f HybridFS) Open(name string) (fs.File, error) {
log.Printf("serving static file: %s\n", name)
var file fs.File
var err error
for _, candidate := range f.fs {
file, err = candidate.Open(name)
if err == nil {
log.Println("succeeded")
return file, nil
}
}
log.Printf("failed: %v\n", err)
return nil, err
}
// InitFS initializes the global HybridFS singleton with the local wiki.
func InitFS(localPath string) {
FS = HybridFS{
fs: []fs.FS{
os.DirFS(localPath),
embedFS,
},
}
}