mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
web: add option to serve live filesystem instead of embedded one
This commit is contained in:
@@ -4,6 +4,8 @@ package web
|
||||
type Configuration struct {
|
||||
// GrafanaURL is the URL to acess Grafana.
|
||||
GrafanaURL string
|
||||
// ServeLiveFS serve files from the filesystem instead of the embedded versions.
|
||||
ServeLiveFS bool
|
||||
}
|
||||
|
||||
// DefaultConfiguration represents the default configuration for the web exporter.
|
||||
|
||||
20
web/root.go
20
web/root.go
@@ -9,6 +9,10 @@ import (
|
||||
netHTTP "net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"akvorado/http"
|
||||
"akvorado/reporter"
|
||||
@@ -38,11 +42,20 @@ func New(reporter *reporter.Reporter, config Configuration, dependencies Depende
|
||||
d: &dependencies,
|
||||
config: config,
|
||||
}
|
||||
data, err := fs.Sub(rootSite, "data")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get embedded website: %w", err)
|
||||
|
||||
var data fs.FS
|
||||
if config.ServeLiveFS {
|
||||
_, src, _, _ := runtime.Caller(0)
|
||||
data = os.DirFS(filepath.Join(path.Dir(src), "data"))
|
||||
} else {
|
||||
var err error
|
||||
data, err = fs.Sub(rootSite, "data")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get embedded website: %w", err)
|
||||
}
|
||||
}
|
||||
c.d.HTTP.AddHandler("/", netHTTP.FileServer(netHTTP.FS(data)))
|
||||
|
||||
if c.config.GrafanaURL != "" {
|
||||
// Provide a proxy for Grafana
|
||||
url, err := url.Parse(config.GrafanaURL)
|
||||
@@ -63,5 +76,6 @@ func New(reporter *reporter.Reporter, config Configuration, dependencies Depende
|
||||
})
|
||||
c.d.HTTP.AddHandler("/grafana/", proxyHandler)
|
||||
}
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
@@ -47,3 +47,31 @@ func TestProxy(t *testing.T) {
|
||||
t.Errorf("GET /grafana/test (-got, +want):\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticFiles(t *testing.T) {
|
||||
for _, live := range []bool{false, true} {
|
||||
name := "livefs"
|
||||
if !live {
|
||||
name = "embeddedfs"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
r := reporter.NewMock(t)
|
||||
h := http.NewMock(t, r)
|
||||
_, err := New(r, Configuration{
|
||||
ServeLiveFS: live,
|
||||
}, Dependencies{HTTP: h})
|
||||
if err != nil {
|
||||
t.Fatalf("New() error:\n%+v", err)
|
||||
}
|
||||
|
||||
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/install.html", h.Address))
|
||||
if err != nil {
|
||||
t.Fatalf("GET /install.html:\n%+v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("GET /install.html: got status code %d, not 200", resp.StatusCode)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user