Add flag to read assets from local files.

This commit is contained in:
Johannes 'fish' Ziemke 2013-03-21 13:55:59 +01:00
parent 1f80b17cb7
commit bf5d312a74
2 changed files with 17 additions and 5 deletions

View file

@ -40,11 +40,18 @@ func (h *StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Status: "TODO: add status information here",
TargetPools: h.appState.TargetManager.Pools(),
}
templateFile, err := blob.GetFile(blob.TemplateFiles, "status.html")
if err != nil {
log.Fatalf("Could not read template: %s", err)
var t *template.Template
if *localAssets {
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, _ := template.New("status").Parse(string(templateFile))
t.Execute(w, status)
}

View file

@ -27,6 +27,7 @@ import (
// Commandline flags.
var (
listenAddress = flag.String("listenAddress", ":9090", "Address to listen on for web interface.")
localAssets = flag.Bool("localAssets", false, "Read assets/templates from file instead of binary.")
)
func StartServing(appState *appstate.ApplicationState) {
@ -36,7 +37,11 @@ func StartServing(appState *appstate.ApplicationState) {
http.Handle("/status", &StatusHandler{appState: appState})
http.Handle("/api/", gorest.Handle())
http.Handle("/metrics.json", exporter)
http.Handle("/static/", http.StripPrefix("/static/", new(blob.Handler)))
if *localAssets {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static"))))
} else {
http.Handle("/static/", http.StripPrefix("/static/", new(blob.Handler)))
}
go http.ListenAndServe(*listenAddress, nil)
}