2021-06-12 13:51:28 +00:00
|
|
|
package static
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2021-06-23 15:44:27 +00:00
|
|
|
//go:embed *.css *.js *.txt icon
|
2021-06-12 13:51:28 +00:00
|
|
|
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.
|
2021-06-12 13:51:28 +00:00
|
|
|
func InitFS(localPath string) {
|
|
|
|
FS = HybridFS{
|
|
|
|
fs: []fs.FS{
|
|
|
|
os.DirFS(localPath),
|
|
|
|
embedFS,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|