From 50ca4bc9499857063d9227d58b99d00b96d6cac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Rainone?= Date: Wed, 4 Oct 2023 01:04:13 +0200 Subject: [PATCH] STATSVIZ_DEBUG env var selects whether assets are embedded or from directory For development/debugging, STATSVIZ_DEBUG=assets=dir allows to serve assets from the contents of the 'dir' directory. --- statsviz.go | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/statsviz.go b/statsviz.go index 3ab1d671..d2fc219d 100644 --- a/statsviz.go +++ b/statsviz.go @@ -32,6 +32,8 @@ import ( "encoding/json" "fmt" "net/http" + "os" + "path/filepath" "strconv" "strings" "time" @@ -179,13 +181,40 @@ var contentTypes = map[string]string{ "libs/js/tippy.js@6": "text/javascript", } +// Returns an FS serving the embedded assets, or the assets directory if +// STATSVIZ_DEBUG contains the 'asssets' key. +func assetsFS() http.FileSystem { + assets := http.FS(static.Assets) + + vdbg := os.Getenv("STATSVIZ_DEBUG") + if vdbg == "" { + return assets + } + + kvs := strings.Split(vdbg, ";") + for _, kv := range kvs { + k, v, found := strings.Cut(strings.TrimSpace(kv), "=") + if !found { + panic("invalid STATSVIZ_DEBUG value: " + kv) + } + if k == "assets" { + dir := filepath.Join(v) + return http.Dir(dir) + } + } + + return assets +} + // Index returns the index handler, which responds with the Statsviz user // interface HTML page. By default, the handler is served at the path specified // by the root. Use [WithRoot] to change the path. func (s *Server) Index() http.HandlerFunc { prefix := strings.TrimSuffix(s.root, "/") + "/" - assetsFS := http.FileServer(http.FS(static.Assets)) - return http.StripPrefix(prefix, intercept(assetsFS, s.plots.Config())).ServeHTTP + assets := http.FileServer(assetsFS()) + handler := intercept(assets, s.plots.Config()) + + return http.StripPrefix(prefix, handler).ServeHTTP } // Ws returns the WebSocket handler used by Statsviz to send application