Embed web assets into the Go binary
Serve /static from embedded assets when no static path override is configured, add static route coverage, and update Docker/docs to reflect embedded-by-default behavior. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package webterm
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
//go:embed static static/* static/js/* static/icons/*
|
||||
var embeddedStaticAssets embed.FS
|
||||
|
||||
func embeddedStaticFS() (http.FileSystem, bool) {
|
||||
sub, err := fs.Sub(embeddedStaticAssets, "static")
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
return http.FS(sub), true
|
||||
}
|
||||
@@ -1466,6 +1466,8 @@ func (s *LocalServer) Handler() http.Handler {
|
||||
mux.HandleFunc("/", s.handleRoot)
|
||||
if strings.TrimSpace(s.staticPath) != "" {
|
||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(s.staticPath))))
|
||||
} else if staticFS, ok := embeddedStaticFS(); ok {
|
||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(staticFS)))
|
||||
}
|
||||
return s.loggingMiddleware(s.gzipMiddleware(mux))
|
||||
}
|
||||
|
||||
@@ -221,6 +221,15 @@ func TestRootTerminalPageAndSparklineValidation(t *testing.T) {
|
||||
t.Fatalf("unexpected root page: %q", text)
|
||||
}
|
||||
|
||||
respStatic, err := http.Get(httpServer.URL + "/static/manifest.json")
|
||||
if err != nil {
|
||||
t.Fatalf("static request error = %v", err)
|
||||
}
|
||||
_ = respStatic.Body.Close()
|
||||
if respStatic.StatusCode != http.StatusOK {
|
||||
t.Fatalf("expected 200 for static manifest, got %d", respStatic.StatusCode)
|
||||
}
|
||||
|
||||
resp2, err := http.Get(httpServer.URL + "/cpu-sparkline.svg")
|
||||
if err != nil {
|
||||
t.Fatalf("sparkline request error = %v", err)
|
||||
|
||||
Reference in New Issue
Block a user