Skip to content

Commit

Permalink
app/vmalert: support http.pathPrefix flag in UI (#1636)
Browse files Browse the repository at this point in the history
The change makes UI to respect `http.pathPrefix` flag
for API or navigation items links.
  • Loading branch information
hagen1778 committed Sep 21, 2021
1 parent a22aa06 commit ac1abe2
Show file tree
Hide file tree
Showing 3 changed files with 349 additions and 342 deletions.
38 changes: 31 additions & 7 deletions app/vmalert/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,56 @@ import (
"encoding/json"
"fmt"
"net/http"
"path"
"sort"
"strconv"
"strings"
"sync"

"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/tpl"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
)

var (
once = sync.Once{}
apiLinks [][2]string
navItems []tpl.NavItem
)

func initLinks() {
pathPrefix := httpserver.GetPathPrefix()
apiLinks = [][2]string{
{path.Join(pathPrefix, "api/v1/groups"), "list all loaded groups and rules"},
{path.Join(pathPrefix, "api/v1/alerts"), "list all active alerts"},
{path.Join(pathPrefix, "api/v1/groupID/alertID/status"), "get alert status by ID"},
{path.Join(pathPrefix, "metrics"), "list of application metrics"},
{path.Join(pathPrefix, "-/reload"), "reload configuration"},
}
navItems = []tpl.NavItem{
{Name: "vmalert", Url: path.Join(pathPrefix, "/")},
{Name: "Groups", Url: path.Join(pathPrefix, "groups")},
{Name: "Alerts", Url: path.Join(pathPrefix, "/alerts")},
{Name: "Docs", Url: "https://docs.victoriametrics.com/vmalert.html"},
}
}

type requestHandler struct {
m *manager
}

func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool {
once.Do(func() {
initLinks()
})

switch r.URL.Path {
case "/":
if r.Method != "GET" {
return false
}
WriteWelcome(w, [][2]string{
{"/api/v1/groups", "list all loaded groups and rules"},
{"/api/v1/alerts", "list all active alerts"},
{"/api/v1/groupID/alertID/status", "get alert status by ID"},
{"/metrics", "list of application metrics"},
{"/-/reload", "reload configuration"},
})
WriteWelcome(w)
return true
case "/alerts":
WriteListAlerts(w, rh.groupAlerts())
Expand Down
13 changes: 2 additions & 11 deletions app/vmalert/web.qtpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,11 @@
) %}


{% code
var navItems = []tpl.NavItem{
{Name: "vmalert", Url: "/"},
{Name: "Groups", Url: "/groups"},
{Name: "Alerts", Url: "/alerts"},
{Name: "Docs", Url: "https://docs.victoriametrics.com/vmalert.html"},
}
%}

{% func Welcome(pathList [][2]string) %}
{% func Welcome() %}
{%= tpl.Header("vmalert", navItems) %}
<p>
API:<br>
{% for _, p := range pathList %}
{% for _, p := range apiLinks %}
{%code
p, doc := p[0], p[1]
%}
Expand Down

0 comments on commit ac1abe2

Please sign in to comment.