Merge pull request #87 from prometheus/feature/build-in-static-assets

Ship assets in compiled binary.
This commit is contained in:
juliusv 2013-03-20 05:33:31 -07:00
commit e458e18dc5
7 changed files with 96 additions and 4 deletions

1
.gitignore vendored
View file

@ -20,3 +20,4 @@
[568a].out
_cgo_*
core
web/blob/files.go

View file

@ -19,11 +19,14 @@ test: build
go test ./...
build:
./utility/embed-static.sh web/static web/templates | gofmt > web/blob/files.go
cat web/blob/files.go
$(MAKE) -C model
go build ./...
go build -o prometheus.build
clean:
rm -rf web/static/blob/files.go
rm -rf $(TEST_ARTIFACTS)
$(MAKE) -C model clean
-find . -type f -iname '*~' -exec rm '{}' ';'

View file

@ -41,7 +41,7 @@ preparation-stamp: build-dependencies
build-dependencies: build-dependencies-stamp
build-dependencies-stamp: bison cc mercurial protoc goprotobuf gorest go instrumentation leveldb levigo skiplist
build-dependencies-stamp: bison cc mercurial protoc goprotobuf gorest go instrumentation leveldb levigo skiplist vim-common
touch $@
overlay: overlay-stamp
@ -144,6 +144,12 @@ rsync: rsync-stamp
rsync-stamp:
[ -x "$$(which rsync)" ] || $(APT_GET_INSTALL) rsync
vim-common: vim-common-stamp
vim-common-stamp:
$(APT_GET_INSTALL) vim-common
touch $@
test: test-stamp
test-stamp: preparation source
@ -183,4 +189,4 @@ clean:
-rm snappy-$(SNAPPY_VERSION).tar.gz
.PHONY: all bison build-dependencies cc clean go goprotobuf gorest instrumentation leveldb levigo mercurial overlay preparation protoc rsync snappy source test wget
.PHONY: all bison build-dependencies cc clean go goprotobuf gorest instrumentation leveldb levigo mercurial overlay preparation protoc rsync snappy source test wget vim-common

26
utility/embed-static.sh Executable file
View file

@ -0,0 +1,26 @@
#!/bin/sh
set -x
cat <<EOF
package blob
var files = map [string] map [string] []byte {
EOF
CDIR=`pwd`
for dir in $@
do
cd "$dir"
echo "\"`basename $dir`\": {"
find -type f | while read file
do
name=`echo "$file"|sed 's|\.\/||'`
echo "\"$name\": {"
gzip -9 -c "$file" | xxd -p |sed 's/\(..\)/0x\1, /g'
echo "},"
echo
done
echo "},"
cd $CDIR
done
echo '}'

48
web/blob/blob.go Normal file
View file

@ -0,0 +1,48 @@
package blob
import (
"bytes"
"compress/gzip"
"io"
"log"
"net/http"
)
const (
TemplateFiles = "templates"
StaticFiles = "static"
)
func GetFile(bucket string, name string) ([]byte, error) {
reader := bytes.NewReader(files[bucket][name])
gz, err := gzip.NewReader(reader)
if err != nil {
return nil, err
}
var b bytes.Buffer
io.Copy(&b, gz)
gz.Close()
return b.Bytes(), nil
}
type Handler struct{}
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
name := r.URL.String()
if name == "" {
name = "index.html"
}
file, err := GetFile(StaticFiles, name)
if err != nil {
if err != io.EOF {
log.Printf("Could not get file: %s", err)
}
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", http.DetectContentType(file))
w.Write(file)
}

View file

@ -16,7 +16,9 @@ package web
import (
"github.com/prometheus/prometheus/appstate"
"github.com/prometheus/prometheus/retrieval"
"github.com/prometheus/prometheus/web/blob"
"html/template"
"log"
"net/http"
)
@ -38,6 +40,11 @@ func (h *StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Status: "TODO: add status information here",
TargetPools: h.appState.TargetManager.Pools(),
}
t, _ := template.ParseFiles("web/templates/status.html")
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)
}

View file

@ -19,6 +19,7 @@ import (
"github.com/prometheus/client_golang"
"github.com/prometheus/prometheus/appstate"
"github.com/prometheus/prometheus/web/api"
"github.com/prometheus/prometheus/web/blob"
"net/http"
_ "net/http/pprof"
)
@ -35,7 +36,7 @@ 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/", http.FileServer(http.Dir("web/static"))))
http.Handle("/static/", http.StripPrefix("/static/", new(blob.Handler)))
go http.ListenAndServe(*listenAddress, nil)
}