mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-15 07:47:31 -08:00
Merge pull request #271 from prometheus/feature/static-user-assets
Support user-provided static asset directory
This commit is contained in:
commit
b12ddbbc75
|
@ -13,6 +13,8 @@
|
||||||
<a href="/graph">Graph & Console</a>
|
<a href="/graph">Graph & Console</a>
|
||||||
<a href="/">Status</a>
|
<a href="/">Status</a>
|
||||||
<a href="/databases">Databases</a>
|
<a href="/databases">Databases</a>
|
||||||
|
{{ define "user_dashboard_link" }}{{ end }}
|
||||||
|
{{ template "user_dashboard_link" .}}
|
||||||
|
|
||||||
<!-- Help should preferentially be the right-most element. -->
|
<!-- Help should preferentially be the right-most element. -->
|
||||||
<a href="https://github.com/prometheus/prometheus/wiki" target="_blank">Help</a>
|
<a href="https://github.com/prometheus/prometheus/wiki" target="_blank">Help</a>
|
||||||
|
|
38
web/web.go
38
web/web.go
|
@ -31,6 +31,7 @@ import (
|
||||||
var (
|
var (
|
||||||
listenAddress = flag.String("listenAddress", ":9090", "Address to listen on for web interface.")
|
listenAddress = flag.String("listenAddress", ":9090", "Address to listen on for web interface.")
|
||||||
useLocalAssets = flag.Bool("useLocalAssets", false, "Read assets/templates from file instead of binary.")
|
useLocalAssets = flag.Bool("useLocalAssets", false, "Read assets/templates from file instead of binary.")
|
||||||
|
userAssetsPath = flag.String("userAssets", "", "Path to static asset directory, available at /user")
|
||||||
)
|
)
|
||||||
|
|
||||||
type WebService struct {
|
type WebService struct {
|
||||||
|
@ -65,17 +66,24 @@ func (w WebService) ServeForever() error {
|
||||||
exp.Handle("/static/", http.StripPrefix("/static/", new(blob.Handler)))
|
exp.Handle("/static/", http.StripPrefix("/static/", new(blob.Handler)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *userAssetsPath != "" {
|
||||||
|
exp.Handle("/user/", http.StripPrefix("/user/", http.FileServer(http.Dir(*userAssetsPath))))
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("listening on %s", *listenAddress)
|
log.Printf("listening on %s", *listenAddress)
|
||||||
|
|
||||||
return http.ListenAndServe(*listenAddress, exp.DefaultCoarseMux)
|
return http.ListenAndServe(*listenAddress, exp.DefaultCoarseMux)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTemplate(name string) (t *template.Template, err error) {
|
func getLocalTemplate(name string) (*template.Template, error) {
|
||||||
if *useLocalAssets {
|
return template.ParseFiles(
|
||||||
return template.ParseFiles("web/templates/_base.html", fmt.Sprintf("web/templates/%s.html", name))
|
"web/templates/_base.html",
|
||||||
}
|
fmt.Sprintf("web/templates/%s.html", name),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
t = template.New("_base")
|
func getEmbeddedTemplate(name string) (*template.Template, error) {
|
||||||
|
t := template.New("_base")
|
||||||
|
|
||||||
file, err := blob.GetFile(blob.TemplateFiles, "_base.html")
|
file, err := blob.GetFile(blob.TemplateFiles, "_base.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -91,6 +99,26 @@ func getTemplate(name string) (t *template.Template, err error) {
|
||||||
}
|
}
|
||||||
t.Parse(string(file))
|
t.Parse(string(file))
|
||||||
|
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func getTemplate(name string) (t *template.Template, err error) {
|
||||||
|
if *useLocalAssets {
|
||||||
|
t, err = getLocalTemplate(name)
|
||||||
|
} else {
|
||||||
|
t, err = getEmbeddedTemplate(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if *userAssetsPath != "" {
|
||||||
|
// replace "user_dashboard_link" template
|
||||||
|
t.Parse(`{{define "user_dashboard_link"}}<a href="/user">User Dashboard{{end}}`)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue