Skip to content

Commit

Permalink
Addresses route-prefix bug described in prometheus#190
Browse files Browse the repository at this point in the history
Pass in the prefix to the Static handler such that it can be removed
from the Path looked up to serve files relative to the FileSystem's
root.
  • Loading branch information
apghero committed Mar 27, 2019
1 parent bddec46 commit f08d548
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions handler/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ func Ready(ms storage.MetricStore) http.Handler {
// Static serves the static files from the provided http.FileSystem.
//
// The returned handler is already instrumented for Prometheus.
func Static(root http.FileSystem) http.Handler {
func Static(root http.FileSystem, prefix string) http.Handler {
handler := http.FileServer(root)
return promhttp.InstrumentHandlerCounter(
httpCnt.MustCurryWith(prometheus.Labels{"handler": "static"}),
http.FileServer(root),
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = r.URL.Path[len(prefix):]
handler.ServeHTTP(w, r)
}),
)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func main() {
r.POST(pushAPIPath+"/job/:job", handler.Push(ms, false))
r.DELETE(pushAPIPath+"/job/:job", handler.Delete(ms))

r.Handler("GET", *routePrefix+"/static/*filepath", handler.Static(asset.Assets))
r.Handler("GET", *routePrefix+"/static/*filepath", handler.Static(asset.Assets, *routePrefix))

statusHandler := handler.Status(ms, asset.Assets, flags)
r.Handler("GET", *routePrefix+"/status", statusHandler)
Expand Down

0 comments on commit f08d548

Please sign in to comment.