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.
This commit is contained in:
Johannes 'fish' Ziemke 2013-03-27 17:40:01 +01:00
parent a4f0578a7e
commit f27adac848
4 changed files with 52 additions and 30 deletions

View File

@ -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)
}

13
web/templates/_base.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Prometheus</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="static/css/prometheus.css">
</head>
<body>
{{template "content" .}}
</body>
</html>

View File

@ -1,14 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Prometheus Status</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="static/css/prometheus.css">
</head>
<body>
<h2>Status</h2>
{{define "content"}}
<h2>Status</h2>
<div class="grouping_box">
{{.Status}}
</div>
@ -41,5 +32,4 @@
{{end}}
</ul>
</div>
</body>
</html>
{{end}}

View File

@ -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)
}