Skip to content

Commit 4f6e1e5

Browse files
authored
Index page should honor configured HTTP path prefix. (#3126)
* Make sure index page honors configured http path prefix. Signed-off-by: Peter Štibraný <[email protected]> * CHANGELOG.md entry Signed-off-by: Peter Štibraný <[email protected]> * Review feedback. Signed-off-by: Peter Štibraný <[email protected]> * Review feedback. Signed-off-by: Peter Štibraný <[email protected]>
1 parent 8178cb2 commit 4f6e1e5

File tree

5 files changed

+64
-20
lines changed

5 files changed

+64
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* [BUGFIX] An index optimisation actually slows things down when using caching. Moved it to the right location. #2973
4646
* [BUGFIX] Ingester: If push request contained both valid and invalid samples, valid samples were ingested but not stored to WAL of the chunks storage. This has been fixed. #3067
4747
* [BUGFIX] Ruler: Config API would return both the `record` and `alert` in `YAML` response keys even when one of them must be empty. #3120
48+
* [BUGFIX] Index page now uses configured HTTP path prefix when creating links. #3126
4849

4950
## 1.3.0 / 2020-08-21
5051

pkg/api/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ func (a *API) RegisterAlertmanager(am *alertmanager.MultitenantAlertmanager, tar
166166
}
167167

168168
// RegisterAPI registers the standard endpoints associated with a running Cortex.
169-
func (a *API) RegisterAPI(cfg interface{}) {
169+
func (a *API) RegisterAPI(httpPathPrefix string, cfg interface{}) {
170170
a.RegisterRoute("/config", configHandler(cfg), false)
171-
a.RegisterRoute("/", http.HandlerFunc(indexHandler), false)
171+
a.RegisterRoute("/", indexHandler(httpPathPrefix), false)
172172
}
173173

174174
// RegisterDistributor registers the endpoints associated with the distributor.

pkg/api/handlers.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package api
22

33
import (
4+
"html/template"
45
"net/http"
6+
"path"
57

68
"github.com/go-kit/kit/log/level"
79
"gopkg.in/yaml.v2"
@@ -10,7 +12,7 @@ import (
1012
)
1113

1214
// TODO: Update this content to be a template that is dynamic based on how Cortex is run.
13-
const indexPageContent = `
15+
var indexPageContent = template.Must(template.New("main").Parse(`
1416
<!DOCTYPE html>
1517
<html>
1618
<head>
@@ -21,29 +23,39 @@ const indexPageContent = `
2123
<h1>Cortex</h1>
2224
<p>Admin Endpoints:</p>
2325
<ul>
24-
<li><a href="/config">Current Config</a></li>
25-
<li><a href="/distributor/all_user_stats">Usage Statistics</a></li>
26-
<li><a href="/distributor/ha_tracker">HA Tracking Status</a></li>
27-
<li><a href="/multitenant_alertmanager/status">Alertmanager Status</a></li>
28-
<li><a href="/ingester/ring">Ingester Ring Status</a></li>
29-
<li><a href="/ruler/ring">Ruler Ring Status</a></li>
30-
<li><a href="/services">Service Status</a></li>
31-
<li><a href="/compactor/ring">Compactor Ring Status (experimental blocks storage)</a></li
32-
<li><a href="/store-gateway/ring">Store Gateway Ring (experimental blocks storage)</a></li>
26+
<li><a href="{{ .AddPathPrefix "/config" }}">Current Config</a></li>
27+
<li><a href="{{ .AddPathPrefix "/distributor/all_user_stats" }}">Usage Statistics</a></li>
28+
<li><a href="{{ .AddPathPrefix "/distributor/ha_tracker" }}">HA Tracking Status</a></li>
29+
<li><a href="{{ .AddPathPrefix "/multitenant_alertmanager/status" }}">Alertmanager Status</a></li>
30+
<li><a href="{{ .AddPathPrefix "/ingester/ring" }}">Ingester Ring Status</a></li>
31+
<li><a href="{{ .AddPathPrefix "/ruler/ring" }}">Ruler Ring Status</a></li>
32+
<li><a href="{{ .AddPathPrefix "/services" }}">Service Status</a></li>
33+
<li><a href="{{ .AddPathPrefix "/compactor/ring" }}">Compactor Ring Status (experimental blocks storage)</a></li>
34+
<li><a href="{{ .AddPathPrefix "/store-gateway/ring" }}">Store Gateway Ring (experimental blocks storage)</a></li>
3335
</ul>
3436
3537
<p>Dangerous:</p>
3638
<ul>
37-
<li><a href="/ingester/flush">Trigger a Flush</a></li>
38-
<li><a href="/ingester/shutdown">Trigger Ingester Shutdown</a></li>
39+
<li><a href="{{ .AddPathPrefix "/ingester/flush" }}">Trigger a Flush</a></li>
40+
<li><a href="{{ .AddPathPrefix "/ingester/shutdown" }}">Trigger Ingester Shutdown</a></li>
3941
</ul>
4042
</body>
41-
</html>`
43+
</html>`))
4244

43-
func indexHandler(w http.ResponseWriter, _ *http.Request) {
44-
if _, err := w.Write([]byte(indexPageContent)); err != nil {
45-
http.Error(w, err.Error(), http.StatusInternalServerError)
46-
return
45+
type indexPageInput struct {
46+
pathPrefix string
47+
}
48+
49+
func (i indexPageInput) AddPathPrefix(p string) string {
50+
return path.Join(i.pathPrefix, p)
51+
}
52+
53+
func indexHandler(httpPathPrefix string) http.HandlerFunc {
54+
return func(w http.ResponseWriter, r *http.Request) {
55+
err := indexPageContent.Execute(w, indexPageInput{pathPrefix: httpPathPrefix})
56+
if err != nil {
57+
http.Error(w, err.Error(), http.StatusInternalServerError)
58+
}
4759
}
4860
}
4961

pkg/api/handlers_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package api
2+
3+
import (
4+
"net/http/httptest"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestIndexHandlerPrefix(t *testing.T) {
12+
for _, tc := range []struct {
13+
prefix string
14+
toBeFound string
15+
}{
16+
{prefix: "", toBeFound: "<a href=\"/ingester/ring\">"},
17+
{prefix: "/test", toBeFound: "<a href=\"/test/ingester/ring\">"},
18+
// All the extra slashed are cleaned up in the result.
19+
{prefix: "///test///", toBeFound: "<a href=\"/test/ingester/ring\">"},
20+
} {
21+
h := indexHandler(tc.prefix)
22+
23+
req := httptest.NewRequest("GET", "/", nil)
24+
resp := httptest.NewRecorder()
25+
26+
h.ServeHTTP(resp, req)
27+
28+
require.Equal(t, 200, resp.Code)
29+
require.True(t, strings.Contains(resp.Body.String(), tc.toBeFound))
30+
}
31+
}

pkg/cortex/modules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (t *Cortex) initAPI() (services.Service, error) {
8080

8181
t.API = a
8282

83-
t.API.RegisterAPI(t.Cfg)
83+
t.API.RegisterAPI(t.Cfg.Server.PathPrefix, t.Cfg)
8484

8585
return nil, nil
8686
}

0 commit comments

Comments
 (0)