Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from go-bindata to vfsgen #198

Merged
merged 1 commit into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 1 addition & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ STATICCHECK_IGNORE = \

DOCKER_IMAGE_NAME ?= pushgateway

ifdef DEBUG
bindata_flags = -debug
endif

assets:
@echo ">> writing assets"
@$(GO) get -u github.com/jteeuwen/go-bindata/...
@go-bindata $(bindata_flags) -prefix=resources resources/...

style:
@echo ">> checking code style"
! $(GOFMT) -d $$(find . -path ./vendor -prune -o -path ./bindata.go -prune -o -name '*.go' -print) | grep '^'
@cd $(PREFIX)/asset && $(GO) generate && $(GOFMT) -w assets_vfsdata.go
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ contrast to how the deprecated form of the path is treated during pushing).

The normal binary embeds the web files in the `resources` directory.
For development purposes, it is handy to have a running binary use
those files directly (so that you can see the effect of changes
immediately). To switch to direct usage, type `DEBUG=1 make assets`
just before compiling the binary. Switch back to "normal" mode by
typing `make assets`.
those files directly (so that you can see the effect of changes immediately).
To switch to direct usage, add `-tags dev` to the `flags` entry in
`.promu.yml`, and then `make build`. Switch back to "normal" mode by
reverting the changes to `.promu.yml` and typing `make assets`.

## Contributing

Expand Down
33 changes: 33 additions & 0 deletions asset/asset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build dev

package asset

import (
"go/build"
"log"
"net/http"
)

func importPathToDir(importPath string) string {
p, err := build.Import(importPath, "", build.FindOnly)
if err != nil {
log.Fatalln(err)
}
return p.Dir
}

// Assets contains the project's assets.
var Assets http.FileSystem = http.Dir(importPathToDir("github.com/prometheus/pushgateway/resources"))
34 changes: 34 additions & 0 deletions asset/asset_generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build ignore

package main

import (
"log"

"github.com/prometheus/pushgateway/asset"
"github.com/shurcooL/vfsgen"
)

func main() {
err := vfsgen.Generate(asset.Assets, vfsgen.Options{
PackageName: "asset",
BuildTags: "!dev",
VariableName: "Assets",
})
if err != nil {
log.Fatalln(err)
}
}
351 changes: 351 additions & 0 deletions asset/assets_vfsdata.go

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions asset/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package asset provides the assets via a virtual filesystem.
package asset

import (
// The blank import is to make govendor happy.
_ "github.com/shurcooL/vfsgen"
)

//go:generate go run -tags=dev asset_generate.go
521 changes: 0 additions & 521 deletions bindata.go

This file was deleted.

12 changes: 10 additions & 2 deletions handler/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"html"
"html/template"
"io/ioutil"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (data) FormatTimestamp(ts int64) string {
// Status serves the status page.
func Status(
ms storage.MetricStore,
assetFunc func(string) ([]byte, error),
root http.FileSystem,
flags map[string]string,
) func(http.ResponseWriter, *http.Request) {
birth := time.Now()
Expand All @@ -59,12 +60,19 @@ func Status(
return strconv.FormatFloat(f, 'f', -1, 64)
},
})
tpl, err := assetFunc("template.html")
f, err := root.Open("template.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Errorf("Error loading template.html, %v", err.Error())
return
}
defer f.Close()
tpl, err := ioutil.ReadAll(f)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Errorf("Error reading template.html, %v", err.Error())
return
}
_, err = t.Parse(string(tpl))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
14 changes: 3 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"syscall"
"time"

"github.com/elazarl/go-bindata-assetfs"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
Expand All @@ -34,6 +33,7 @@ import (

dto "github.com/prometheus/client_model/go"

"github.com/prometheus/pushgateway/asset"
"github.com/prometheus/pushgateway/handler"
"github.com/prometheus/pushgateway/storage"
)
Expand Down Expand Up @@ -105,16 +105,8 @@ func main() {

r.Handler("GET", *routePrefix+"/static/*filepath", prometheus.InstrumentHandler(
"static",
http.FileServer(
&assetfs.AssetFS{
Asset: func(name string) ([]byte, error) {
path := name[len(*routePrefix):]
return Asset(path)
},
AssetDir: AssetDir, AssetInfo: AssetInfo},
),
))
statusHandler := prometheus.InstrumentHandlerFunc("status", handler.Status(ms, Asset, flags))
http.FileServer(asset.Assets)))
statusHandler := prometheus.InstrumentHandlerFunc("status", handler.Status(ms, asset.Assets, flags))
r.Handler("GET", *routePrefix+"/status", statusHandler)
r.Handler("GET", *routePrefix+"/", statusHandler)

Expand Down
23 changes: 0 additions & 23 deletions vendor/github.com/elazarl/go-bindata-assetfs/LICENSE

This file was deleted.

46 changes: 0 additions & 46 deletions vendor/github.com/elazarl/go-bindata-assetfs/README.md

This file was deleted.