From 48c41ebfb69e8eca01c2b1cea1c2ca8fc4ce808e Mon Sep 17 00:00:00 2001 From: Paul Gier Date: Mon, 2 Mar 2020 14:55:03 -0600 Subject: [PATCH] improve web.route-prefix handling (#2208) This makes the handling of web.route-prefix more similar to the behavior in Prometheus. Correctly handles '/' and prefixes which do not begin with a '/'. Signed-off-by: Paul Gier --- cmd/thanos/query.go | 9 +++++++-- cmd/thanos/rule.go | 8 ++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/cmd/thanos/query.go b/cmd/thanos/query.go index 9433f5601d..ddbb315d2a 100644 --- a/cmd/thanos/query.go +++ b/cmd/thanos/query.go @@ -9,6 +9,7 @@ import ( "math" "net/http" "path" + "strings" "time" "github.com/go-kit/kit/log" @@ -324,11 +325,15 @@ func runQuery( { router := route.New() + // RoutePrefix must always start with '/'. + webRoutePrefix = "/" + strings.Trim(webRoutePrefix, "/") + // Redirect from / to /webRoutePrefix. - if webRoutePrefix != "" { + if webRoutePrefix != "/" { router.Get("/", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, webRoutePrefix, http.StatusFound) }) + router = router.WithPrefix(webRoutePrefix) } flagsMap := map[string]string{ @@ -338,7 +343,7 @@ func runQuery( } ins := extpromhttp.NewInstrumentationMiddleware(reg) - ui.NewQueryUI(logger, reg, stores, flagsMap).Register(router.WithPrefix(webRoutePrefix), ins) + ui.NewQueryUI(logger, reg, stores, flagsMap).Register(router, ins) api := v1.NewAPI(logger, reg, engine, queryableCreator, enableAutodownsampling, enablePartialResponse, replicaLabels, instantDefaultMaxSourceResolution) diff --git a/cmd/thanos/rule.go b/cmd/thanos/rule.go index 9db7ba17e7..a9dcc857be 100644 --- a/cmd/thanos/rule.go +++ b/cmd/thanos/rule.go @@ -558,11 +558,15 @@ func runRule( { router := route.New() + // RoutePrefix must always start with '/'. + webRoutePrefix = "/" + strings.Trim(webRoutePrefix, "/") + // Redirect from / to /webRoutePrefix. - if webRoutePrefix != "" { + if webRoutePrefix != "/" { router.Get("/", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, webRoutePrefix, http.StatusFound) }) + router = router.WithPrefix(webRoutePrefix) } router.WithPrefix(webRoutePrefix).Post("/-/reload", func(w http.ResponseWriter, r *http.Request) { @@ -577,7 +581,7 @@ func runRule( ins := extpromhttp.NewInstrumentationMiddleware(reg) - ui.NewRuleUI(logger, reg, ruleMgr, alertQueryURL.String(), flagsMap).Register(router.WithPrefix(webRoutePrefix), ins) + ui.NewRuleUI(logger, reg, ruleMgr, alertQueryURL.String(), flagsMap).Register(router, ins) api := v1.NewAPI(logger, reg, ruleMgr) api.Register(router.WithPrefix(path.Join(webRoutePrefix, "/api/v1")), tracer, logger, ins)