fork/gdu

git clone https://git.julia.blue/fork/gdu

Daniel Mildefeat: add web UI (#617)d672bf4

ignore
970 B39 linesraw
1package webui
2
3import (
4	"embed"
5	"io/fs"
6	"net/http"
7	"path"
8	"strings"
9)
10
11// distFS holds the compiled, minified frontend bundle. The dist directory is
12// committed to the repository so that pure-Go builds (including `go install`)
13// work without a Node toolchain. Run `make build-web` to regenerate it.
14//
15//go:embed all:dist
16var distFS embed.FS
17
18// staticHandler serves the embedded SPA. Unknown non-asset paths fall back to
19// index.html so client-side routing works.
20func staticHandler() http.Handler {
21	sub, err := fs.Sub(distFS, "dist")
22	if err != nil {
23		panic(err)
24	}
25	fileServer := http.FileServer(http.FS(sub))
26
27	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
28		reqPath := strings.TrimPrefix(path.Clean(r.URL.Path), "/")
29		if reqPath == "" {
30			reqPath = "index.html"
31		}
32
33		if _, err := fs.Stat(sub, reqPath); err != nil {
34			// Not a real asset: serve the SPA entry point.
35			r.URL.Path = "/"
36		}
37		fileServer.ServeHTTP(w, r)
38	})
39}