From f27adac8485c280cad7d2b4b8c943f709460da0d Mon Sep 17 00:00:00 2001 From: Johannes 'fish' Ziemke Date: Wed, 27 Mar 2013 17:40:01 +0100 Subject: [PATCH] Split status template into two templates. This is now using a base template (_base.html) for the 'layout' of the web UI. Within that base template, the actual content templates get rendered. --- web/status.go | 18 +----------------- web/templates/_base.html | 13 +++++++++++++ web/templates/status.html | 16 +++------------- web/web.go | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 30 deletions(-) create mode 100644 web/templates/_base.html diff --git a/web/status.go b/web/status.go index 07f310b3d..2874fe9d7 100644 --- a/web/status.go +++ b/web/status.go @@ -16,9 +16,6 @@ package web import ( "github.com/prometheus/prometheus/appstate" "github.com/prometheus/prometheus/retrieval" - "github.com/prometheus/prometheus/web/blob" - "html/template" - "log" "net/http" ) @@ -40,18 +37,5 @@ func (h *StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { Status: "TODO: add status information here", TargetPools: h.appState.TargetManager.Pools(), } - - var t *template.Template - if *useLocalAssets { - t, _ = template.ParseFiles("web/templates/status.html") - } else { - templateFile, err := blob.GetFile(blob.TemplateFiles, "status.html") - if err != nil { - log.Fatalf("Could not read template: %s", err) - } - - t, _ = template.New("status").Parse(string(templateFile)) - } - - t.Execute(w, status) + executeTemplate(w, "status", status) } diff --git a/web/templates/_base.html b/web/templates/_base.html new file mode 100644 index 000000000..02f80d234 --- /dev/null +++ b/web/templates/_base.html @@ -0,0 +1,13 @@ + + + + + Prometheus + + + + + + {{template "content" .}} + + diff --git a/web/templates/status.html b/web/templates/status.html index 62712d5c2..a7602dab4 100644 --- a/web/templates/status.html +++ b/web/templates/status.html @@ -1,14 +1,5 @@ - - - - - Prometheus Status - - - - - -

Status

+{{define "content"}} +

Status

{{.Status}}
@@ -41,5 +32,4 @@ {{end}} - - +{{end}} diff --git a/web/web.go b/web/web.go index e3672a707..6c9b55e2d 100644 --- a/web/web.go +++ b/web/web.go @@ -20,6 +20,8 @@ import ( "github.com/prometheus/prometheus/appstate" "github.com/prometheus/prometheus/web/api" "github.com/prometheus/prometheus/web/blob" + "html/template" + "log" "net/http" _ "net/http/pprof" ) @@ -45,3 +47,36 @@ func StartServing(appState *appstate.ApplicationState) { go http.ListenAndServe(*listenAddress, nil) } + +func getTemplate(name string) (t *template.Template, err error) { + if *useLocalAssets { + return template.ParseFiles("web/templates/_base.html", "web/templates/"+name+".html") + } + + t = template.New("_base") + + file, err := blob.GetFile(blob.TemplateFiles, "_base.html") + if err != nil { + log.Printf("Could not read base template: %s", err) + return nil, err + } + t.Parse(string(file)) + + file, err = blob.GetFile(blob.TemplateFiles, name+".html") + if err != nil { + log.Printf("Could not read %s template: %s", name, err) + return nil, err + } + t.Parse(string(file)) + + return +} + +func executeTemplate(w http.ResponseWriter, name string, data interface{}) { + tpl, err := getTemplate(name) + if err != nil { + log.Printf("Errror preparing layout template: %s", err) + return + } + tpl.Execute(w, data) +}