beerpong-elo/internal/web/staticFiles.go
2025-02-02 15:11:35 +01:00

29 lines
494 B
Go

package web
import (
"embed"
"io/fs"
"net/http"
"os"
)
//go:embed static/*
var staticFiles embed.FS
type spaFileSystem struct {
root http.FileSystem
}
func (spa *spaFileSystem) Open(name string) (http.File, error) {
f, err := spa.root.Open(name)
if os.IsNotExist(err) {
return spa.root.Open("index.html")
}
return f, err
}
func initStatic(mux *http.ServeMux) {
staticFS, _ := fs.Sub(staticFiles, "static")
mux.Handle("/", http.FileServer(&spaFileSystem{http.FS(staticFS)}))
}