Files

46 lines
1.0 KiB
Go

package api
import (
"embed"
"io/fs"
"net/http"
)
//go:embed web/index.html web/app.css web/app.js web/leaflet.css web/leaflet.js
var webAssets embed.FS
func (s *Server) registerWebRoutes(mux *http.ServeMux) {
assets, err := fs.Sub(webAssets, "web")
if err != nil {
panic(err)
}
fileServer := http.FileServer(http.FS(assets))
mux.Handle("/assets/", http.StripPrefix("/assets/", noCache(fileServer)))
mux.HandleFunc("/", s.handleDashboard)
}
func (s *Server) handleDashboard(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
index, err := webAssets.ReadFile("web/index.html")
if err != nil {
http.Error(w, "dashboard not available", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
_, _ = w.Write(index)
}
func noCache(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
next.ServeHTTP(w, r)
})
}