From 034ab0da0c3994820a2a316a529535f269ca6738 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Wed, 11 Nov 2015 15:34:36 +0100 Subject: [PATCH] Statically compile assets into binary --- Makefile | 10 +++++++++- ui/app/index.html | 16 +++++++-------- web.go | 51 +++++++++++++++++++++++++++++++---------------- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 1cb0fb96..270fe2c0 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,10 @@ GO := GO15VENDOREXPERIMENT=1 go pkgs = $(shell $(GO) list ./... | grep -v /vendor/) +ifdef DEBUG + bindata_flags = -debug +endif + all: format build test test: @@ -28,9 +32,13 @@ vet: @echo ">> vetting code" @$(GO) vet $(pkgs) -build: +build: assets @echo ">> building binaries" @./scripts/build.sh +assets: + @echo ">> writing assets" + @$(GO) get -u github.com/jteeuwen/go-bindata/... + @go-bindata $(bindata_flags) -pkg ui -o ui/bindata.go ui/... .PHONY: all format build test vet docker diff --git a/ui/app/index.html b/ui/app/index.html index f6a391ec..eb601678 100644 --- a/ui/app/index.html +++ b/ui/app/index.html @@ -1,18 +1,18 @@ - - + + - - - - - + + + + + - + AlertManager – Prometheus diff --git a/web.go b/web.go index 190ea08a..317b18ad 100644 --- a/web.go +++ b/web.go @@ -14,30 +14,47 @@ package main import ( - "io/ioutil" + "bytes" + "io" "net/http" - "os" + "path/filepath" + "github.com/prometheus/common/log" "github.com/prometheus/common/route" + + "github.com/prometheus/alertmanager/ui" ) +func serveAsset(w http.ResponseWriter, req *http.Request, fp string) { + info, err := ui.AssetInfo(fp) + if err != nil { + log.Warn("Could not get file: ", err) + w.WriteHeader(http.StatusNotFound) + return + } + file, err := ui.Asset(fp) + if err != nil { + if err != io.EOF { + log.With("file", fp).Warn("Could not get file: ", err) + } + w.WriteHeader(http.StatusNotFound) + return + } + + http.ServeContent(w, req, info.Name(), info.ModTime(), bytes.NewReader(file)) +} + // RegisterWeb registers handlers to serve files for the web interface. func RegisterWeb(r *route.Router) { - - r.Get("/app/*filepath", route.FileServe("ui/app/")) - r.Get("/static/*filepath", route.FileServe("ui/lib/")) - + r.Get("/app/*filepath", func(w http.ResponseWriter, req *http.Request) { + fp := route.Param(route.Context(req), "filepath") + serveAsset(w, req, filepath.Join("ui/app", fp)) + }) + r.Get("/lib/*filepath", func(w http.ResponseWriter, req *http.Request) { + fp := route.Param(route.Context(req), "filepath") + serveAsset(w, req, filepath.Join("ui/lib", fp)) + }) r.Get("/", func(w http.ResponseWriter, req *http.Request) { - f, err := os.Open("ui/app/index.html") - if err != nil { - panic(err) - } - defer f.Close() - - b, err := ioutil.ReadAll(f) - if err != nil { - panic(err) - } - w.Write(b) + serveAsset(w, req, "ui/app/index.html") }) }