44 lines
953 B
Go
44 lines
953 B
Go
package webui
|
|
|
|
import (
|
|
"embed"
|
|
"io/fs"
|
|
"net/http"
|
|
)
|
|
|
|
//go:embed dist
|
|
var DistFS embed.FS
|
|
|
|
var Dist fs.FS = DistFS
|
|
|
|
func ServeHTTP() http.Handler {
|
|
return http.FileServer(http.FS(DistFS))
|
|
}
|
|
|
|
func ServeSPA() http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
path := r.URL.Path
|
|
if path == "/" || !isStaticAsset(path) {
|
|
data, err := DistFS.ReadFile("dist/index.html")
|
|
if err != nil {
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write(data)
|
|
return
|
|
}
|
|
http.StripPrefix("/", http.FileServer(http.FS(DistFS))).ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func isStaticAsset(path string) bool {
|
|
exts := []string{".js", ".css", ".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico", ".woff", ".woff2", ".ttf", ".eot", ".map"}
|
|
for _, ext := range exts {
|
|
if len(path) > len(ext) && path[len(path)-len(ext):] == ext {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|