1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 11:46:16 +00:00
mycorrhiza/static/static.go
2021-06-12 22:04:30 +07:00

46 lines
843 B
Go

package static
import (
"embed"
"io/fs"
"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) {
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
}
// InitFS initializes the global HybridFS singleton with the local wiki.
func InitFS(localPath string) {
FS = HybridFS{
fs: []fs.FS{
os.DirFS(localPath),
embedFS,
},
}
}