// Package ui serves the embedded dashboard: one HTML shell plus ES modules // and a vendored copy of hls.js. There is no build step, which is why the // JavaScript is plain modules rather than a bundle. package ui import ( "embed" "io/fs" "net/http" "strings" ) //go:embed index.html all:static var files embed.FS // Handler serves the single-page shell for every non-asset path, so the hash // router can own navigation. func Handler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" && r.URL.Path != "/index.html" { http.NotFound(w, r) return } b, err := files.ReadFile("index.html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") // The shell references hashed-by-content assets only loosely, so keep // it uncached to avoid serving a stale page against new modules. w.Header().Set("Cache-Control", "no-cache") _, _ = w.Write(b) }) } // Static serves the dashboard assets. Directory listings are refused: the // asset tree is not interesting, and an index is one more thing to leak. func Static() http.Handler { sub, err := fs.Sub(files, "static") if err != nil { panic(err) } fileServer := http.FileServer(http.FS(sub)) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "" || r.URL.Path == "/" || strings.HasSuffix(r.URL.Path, "/") { http.NotFound(w, r) return } // The vendored library is immutable for a given version; the rest is // revalidated so an updated build is picked up on reload. if strings.HasPrefix(r.URL.Path, "/vendor/") { w.Header().Set("Cache-Control", "public, max-age=86400") } else { w.Header().Set("Cache-Control", "no-cache") } fileServer.ServeHTTP(w, r) }) }