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 <roman@victoriametrics.com>

* vmalert: fix review comments

Signed-off-by: hagen1778 <roman@victoriametrics.com>

* Update app/vmselect/main.go

Co-authored-by: Aliaksandr Valialkin <valyala@victoriametrics.com>
This commit is contained in:
Roman Khavronenko 2022-07-11 18:52:22 +02:00 committed by GitHub
parent 5c8eee26bf
commit 8667307d73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 4 deletions

View File

@ -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/"))

View File

@ -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) {

View File

@ -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)