From 8667307d7349a245608992eea513ee55dbfd45a5 Mon Sep 17 00:00:00 2001 From: Roman Khavronenko Date: Mon, 11 Jul 2022 18:52:22 +0200 Subject: [PATCH] vmselect: cover special cases for vmalert's routing in single-node version (#2845) * vmselect: cover special cases for vmalert's routing in single-node version * remove trailing `/` from requests * redirect to vmalert's home page when `/vmalert` is requested. Signed-off-by: hagen1778 * vmalert: fix review comments Signed-off-by: hagen1778 * Update app/vmselect/main.go Co-authored-by: Aliaksandr Valialkin --- app/vmalert/web.go | 6 ++++-- app/vmalert/web_test.go | 1 - app/vmselect/main.go | 6 +++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/vmalert/web.go b/app/vmalert/web.go index ae9c15616..8ca37270c 100644 --- a/app/vmalert/web.go +++ b/app/vmalert/web.go @@ -37,7 +37,7 @@ func initLinks() { {"/-/reload", "reload configuration"}, } navItems = []tpl.NavItem{ - {Name: "vmalert", Url: "home"}, + {Name: "vmalert", Url: "."}, {Name: "Groups", Url: "groups"}, {Name: "Alerts", Url: "alerts"}, {Name: "Notifiers", Url: "notifiers"}, @@ -67,8 +67,9 @@ func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool { } switch r.URL.Path { - case "/", "/vmalert", "/vmalert/home": + case "/", "/vmalert", "/vmalert/": if r.Method != "GET" { + httpserver.Errorf(w, r, "path %q supports only GET method", r.URL.Path) return false } WriteWelcome(w, r) @@ -146,6 +147,7 @@ func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool { // TODO: to remove in next versions if !strings.HasSuffix(r.URL.Path, "/status") { + httpserver.Errorf(w, r, "unsupported path requested: %q ", r.URL.Path) return false } alert, err := rh.alertByPath(strings.TrimPrefix(r.URL.Path, "/api/v1/")) diff --git a/app/vmalert/web_test.go b/app/vmalert/web_test.go index af77967ec..6271c2a64 100644 --- a/app/vmalert/web_test.go +++ b/app/vmalert/web_test.go @@ -52,7 +52,6 @@ func TestHandler(t *testing.T) { t.Run("/", func(t *testing.T) { getResp(ts.URL, nil, 200) getResp(ts.URL+"/vmalert", nil, 200) - getResp(ts.URL+"/vmalert/home", nil, 200) }) t.Run("/api/v1/alerts", func(t *testing.T) { diff --git a/app/vmselect/main.go b/app/vmselect/main.go index ee662d546..d748282d2 100644 --- a/app/vmselect/main.go +++ b/app/vmselect/main.go @@ -212,7 +212,11 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) bool { return true } - if strings.HasPrefix(path, "/vmalert") { + if path == "/vmalert" { + http.Redirect(w, r, path+"/", http.StatusMovedPermanently) + return true + } + if strings.HasPrefix(path, "/vmalert/") { vmalertRequests.Inc() if len(*vmalertProxyURL) == 0 { w.WriteHeader(http.StatusBadRequest)