fork/gdu
git clone https://git.julia.blue/fork/gdu
d672bf4
master
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 { 21sub ,err := fs .Sub (distFS ,"dist" ) 22if err != nil { 23panic (err ) 24 } 25fileServer := http .FileServer (http .FS (sub )) 26 27return http .HandlerFunc (func (w http.ResponseWriter ,r * http.Request ) { 28reqPath := strings .TrimPrefix (path .Clean (r .URL .Path ),"/" ) 29if reqPath == "" { 30reqPath = "index.html" 31 } 32 33if _ ,err := fs .Stat (sub ,reqPath );err != nil { 34// Not a real asset: serve the SPA entry point. 35r .URL .Path = "/" 36 } 37fileServer .ServeHTTP (w ,r ) 38 }) 39}