1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 19:56:16 +00:00
mycorrhiza/static/static.go

47 lines
934 B
Go
Raw Normal View History

package static
import (
"embed"
"io/fs"
"os"
)
//go:embed *.css *.js *.txt 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) {
var file fs.File
var err error
for _, candidate := range f.fs {
file, err = candidate.Open(name)
if err == nil {
return file, nil
}
}
return nil, err
}
2021-06-23 15:46:04 +00:00
// InitFS initializes the global HybridFS singleton with the wiki's own static
// files directory as a primary filesystem and the embedded one as a fallback.
func InitFS(localPath string) {
FS = HybridFS{
fs: []fs.FS{
os.DirFS(localPath),
embedFS,
},
}
}