newfetch/dist/static.go
2022-07-06 23:07:24 +08:00

34 lines
667 B
Go

package dist
import (
"embed"
"errors"
"io/fs"
"path/filepath"
"strings"
)
type Dist struct {
embed.FS
}
//go:embed css js favicon.ico index.html
var Static embed.FS
func (r Dist) Open(name string) (fs.File, error) {
if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) {
return nil, errors.New("http: invalid character in file path")
}
fullName := strings.TrimLeft(name, "/")
prifix := strings.Split(fullName, ".")
l := len(prifix)
p := prifix[l-1]
if p == "js" || p == "css" {
fullName = p + "/" + fullName
} else if p == "map" {
fullName = "js/" + fullName
}
file, err := r.FS.Open(fullName)
return file, err
}