From 74920010d6bf81f33d1dfcb8443dbdca07ae7b88 Mon Sep 17 00:00:00 2001 From: Prem Saraswat Date: Sun, 9 May 2021 15:07:19 +0530 Subject: [PATCH] UI: Fix infinite redirection loop on / --- pkg/ui/bucket.go | 7 ++----- pkg/ui/query.go | 8 ++------ pkg/ui/rule.go | 9 ++------- pkg/ui/ui.go | 1 + 4 files changed, 7 insertions(+), 18 deletions(-) diff --git a/pkg/ui/bucket.go b/pkg/ui/bucket.go index 5a198a52af9..e9530c96fdf 100644 --- a/pkg/ui/bucket.go +++ b/pkg/ui/bucket.go @@ -62,11 +62,8 @@ func (b *Bucket) Register(r *route.Router, registerNewUI bool, ins extpromhttp.I // Redirect the original React UI's path (under "/new") to its new path at the root. r.Get("/new/*path", func(w http.ResponseWriter, r *http.Request) { p := route.Param(r.Context(), "path") - http.Redirect(w, r, path.Join(GetWebPrefix(b.logger, b.externalPrefix, b.prefixHeader, r), strings.TrimPrefix(p, "/new"))+"?"+r.URL.RawQuery, http.StatusFound) - }) - - r.Get("/", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, path.Join(GetWebPrefix(b.logger, b.externalPrefix, b.prefixHeader, r), b.uiPrefix), http.StatusFound) + prefix := GetWebPrefix(b.logger, b.externalPrefix, b.prefixHeader, r) + http.Redirect(w, r, path.Join("/", prefix, p)+"?"+r.URL.RawQuery, http.StatusFound) }) registerReactApp(r, ins, b.BaseUI) diff --git a/pkg/ui/query.go b/pkg/ui/query.go index 260169a1900..7389dfbd548 100644 --- a/pkg/ui/query.go +++ b/pkg/ui/query.go @@ -67,18 +67,14 @@ func queryTmplFuncs() template.FuncMap { // Register registers new GET routes for subpages and redirects from / to /graph. func (q *Query) Register(r *route.Router, ins extpromhttp.InstrumentationMiddleware) { - r.Get("/", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, path.Join(GetWebPrefix(q.logger, q.externalPrefix, q.prefixHeader, r), "/graph"), http.StatusFound) - }) - r.Get("/classic/", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, path.Join(GetWebPrefix(q.logger, q.externalPrefix, q.prefixHeader, r), "/classic/graph"), http.StatusFound) + http.Redirect(w, r, path.Join("/", GetWebPrefix(q.logger, q.externalPrefix, q.prefixHeader, r), "/classic/graph"), http.StatusFound) }) // Redirect the original React UI's path (under "/new") to its new path at the root. r.Get("/new/*path", func(w http.ResponseWriter, r *http.Request) { p := route.Param(r.Context(), "path") - http.Redirect(w, r, path.Join(GetWebPrefix(q.logger, q.externalPrefix, q.prefixHeader, r), strings.TrimPrefix(p, "/new"))+"?"+r.URL.RawQuery, http.StatusFound) + http.Redirect(w, r, path.Join("/", GetWebPrefix(q.logger, q.externalPrefix, q.prefixHeader, r), p)+"?"+r.URL.RawQuery, http.StatusFound) }) r.Get("/classic/graph", instrf("graph", ins, q.graph)) diff --git a/pkg/ui/rule.go b/pkg/ui/rule.go index 0a7eb2241d9..01869d2b63e 100644 --- a/pkg/ui/rule.go +++ b/pkg/ui/rule.go @@ -10,7 +10,6 @@ import ( "net/http" "path" "regexp" - "strings" "time" "github.com/go-kit/kit/log" @@ -158,18 +157,14 @@ func (ru *Rule) rules(w http.ResponseWriter, r *http.Request) { } func (ru *Rule) Register(r *route.Router, ins extpromhttp.InstrumentationMiddleware) { - r.Get("/", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, path.Join(GetWebPrefix(ru.logger, ru.externalPrefix, ru.prefixHeader, r), "/alerts"), http.StatusFound) - }) - r.Get("/classic/", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, path.Join(GetWebPrefix(ru.logger, ru.externalPrefix, ru.prefixHeader, r), "/classic/alerts"), http.StatusFound) + http.Redirect(w, r, path.Join("/", GetWebPrefix(ru.logger, ru.externalPrefix, ru.prefixHeader, r), "/classic/alerts"), http.StatusFound) }) // Redirect the original React UI's path (under "/new") to its new path at the root. r.Get("/new/*path", func(w http.ResponseWriter, r *http.Request) { p := route.Param(r.Context(), "path") - http.Redirect(w, r, path.Join(GetWebPrefix(ru.logger, ru.externalPrefix, ru.prefixHeader, r), strings.TrimPrefix(p, "/new"))+"?"+r.URL.RawQuery, http.StatusFound) + http.Redirect(w, r, path.Join("/", GetWebPrefix(ru.logger, ru.externalPrefix, ru.prefixHeader, r), p)+"?"+r.URL.RawQuery, http.StatusFound) }) r.Get("/classic/alerts", instrf("alerts", ins, ru.alerts)) diff --git a/pkg/ui/ui.go b/pkg/ui/ui.go index fbb6f9f6adf..d663c62b15f 100644 --- a/pkg/ui/ui.go +++ b/pkg/ui/ui.go @@ -23,6 +23,7 @@ import ( ) var reactRouterPaths = []string{ + "/", "/alerts", "/blocks", "/config",