Skip to content

Commit

Permalink
STATSVIZ_DEBUG env var selects whether assets are embedded or from di…
Browse files Browse the repository at this point in the history
…rectory

For development/debugging, STATSVIZ_DEBUG=assets=dir allows to serve
assets from the contents of the 'dir' directory.
  • Loading branch information
arl committed Oct 3, 2023
1 parent 9678f65 commit 50ca4bc
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions statsviz.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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)

Check warning on line 198 in statsviz.go

View check run for this annotation

Codecov / codecov/patch

statsviz.go#L194-L198

Added lines #L194 - L198 were not covered by tests
}
if k == "assets" {
dir := filepath.Join(v)
return http.Dir(dir)
}

Check warning on line 203 in statsviz.go

View check run for this annotation

Codecov / codecov/patch

statsviz.go#L200-L203

Added lines #L200 - L203 were not covered by tests
}

return assets

Check warning on line 206 in statsviz.go

View check run for this annotation

Codecov / codecov/patch

statsviz.go#L206

Added line #L206 was not covered by tests
}

// 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
Expand Down

0 comments on commit 50ca4bc

Please sign in to comment.