37 lines
761 B
Go
37 lines
761 B
Go
package web
|
|
|
|
import (
|
|
"embed"
|
|
"io/fs"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed all:dist
|
|
var assets embed.FS
|
|
|
|
func Handler() http.Handler {
|
|
root, _ := fs.Sub(assets, "dist")
|
|
files := http.FileServer(http.FS(root))
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
path := strings.TrimPrefix(r.URL.Path, "/")
|
|
if path == "" {
|
|
path = "index.html"
|
|
}
|
|
if _, err := fs.Stat(root, path); err != nil {
|
|
if strings.Contains(path, ".") {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
r = r.Clone(r.Context())
|
|
r.URL.Path = "/"
|
|
}
|
|
if strings.HasPrefix(path, "assets/") {
|
|
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
|
} else {
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
}
|
|
files.ServeHTTP(w, r)
|
|
})
|
|
}
|