Statically compile assets into binary

This commit is contained in:
Fabian Reinartz 2015-11-11 15:34:36 +01:00
parent f188f3357d
commit 034ab0da0c
3 changed files with 51 additions and 26 deletions

View File

@ -14,6 +14,10 @@
GO := GO15VENDOREXPERIMENT=1 go GO := GO15VENDOREXPERIMENT=1 go
pkgs = $(shell $(GO) list ./... | grep -v /vendor/) pkgs = $(shell $(GO) list ./... | grep -v /vendor/)
ifdef DEBUG
bindata_flags = -debug
endif
all: format build test all: format build test
test: test:
@ -28,9 +32,13 @@ vet:
@echo ">> vetting code" @echo ">> vetting code"
@$(GO) vet $(pkgs) @$(GO) vet $(pkgs)
build: build: assets
@echo ">> building binaries" @echo ">> building binaries"
@./scripts/build.sh @./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 .PHONY: all format build test vet docker

View File

@ -1,18 +1,18 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" ng-app="am"> <html lang="en" ng-app="am">
<head> <head>
<script src="/static/jquery.min.js"></script> <script src="/lib/jquery.min.js"></script>
<script src="/static/moment.min.js"></script> <script src="/lib/moment.min.js"></script>
<script src="/static/angular.min.js"></script> <script src="/lib/angular.min.js"></script>
<script src="/static/angular-sanitize.min.js"></script> <script src="/lib/angular-sanitize.min.js"></script>
<script src="/static/angular-route.min.js"></script> <script src="/lib/angular-route.min.js"></script>
<script src="/static/angular-resource.min.js"></script> <script src="/lib/angular-resource.min.js"></script>
<script src="/static/angular-moment.min.js"></script> <script src="/lib/angular-moment.min.js"></script>
<script src="/app/js/app.js"></script> <script src="/app/js/app.js"></script>
<link rel="stylesheet" href="/static/kube.min.css"> <link rel="stylesheet" href="/lib/kube.min.css">
<link rel="stylesheet" href="/app/css/main.css"> <link rel="stylesheet" href="/app/css/main.css">
<title>AlertManager Prometheus</title> <title>AlertManager Prometheus</title>

51
web.go
View File

@ -14,30 +14,47 @@
package main package main
import ( import (
"io/ioutil" "bytes"
"io"
"net/http" "net/http"
"os" "path/filepath"
"github.com/prometheus/common/log"
"github.com/prometheus/common/route" "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. // RegisterWeb registers handlers to serve files for the web interface.
func RegisterWeb(r *route.Router) { func RegisterWeb(r *route.Router) {
r.Get("/app/*filepath", func(w http.ResponseWriter, req *http.Request) {
r.Get("/app/*filepath", route.FileServe("ui/app/")) fp := route.Param(route.Context(req), "filepath")
r.Get("/static/*filepath", route.FileServe("ui/lib/")) 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) { r.Get("/", func(w http.ResponseWriter, req *http.Request) {
f, err := os.Open("ui/app/index.html") serveAsset(w, req, "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)
}) })
} }