1package webui2
2
3import (
4 "embed"
5 "io/fs"
6 "net/http"
7)
8
9//go:embed all:dist
10var assets embed.FS
11
12// NewHandler returns an http.Handler that serves the webui2 SPA.
13// Unknown paths fall back to index.html so that client-side routing works.
14func NewHandler() http.Handler {
15 dist, err := fs.Sub(assets, "dist")
16 if err != nil {
17 panic(err)
18 }
19 return http.FileServer(&spaFS{http.FS(dist)})
20}
21
22// spaFS wraps an http.FileSystem to serve index.html for any path that does
23// not correspond to a real file, enabling client-side routing in the SPA.
24type spaFS struct {
25 http.FileSystem
26}
27
28func (s *spaFS) Open(name string) (http.File, error) {
29 f, err := s.FileSystem.Open(name)
30 if err != nil {
31 return s.FileSystem.Open("/index.html")
32 }
33 return f, nil
34}