diff --git a/CHANGELOG.md b/CHANGELOG.md index ae0e94521b6..3028057f2e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ * [BUGFIX] An index optimisation actually slows things down when using caching. Moved it to the right location. #2973 * [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 * [BUGFIX] Ruler: Config API would return both the `record` and `alert` in `YAML` response keys even when one of them must be empty. #3120 +* [BUGFIX] Index page now uses configured HTTP path prefix when creating links. #3126 ## 1.3.0 / 2020-08-21 diff --git a/pkg/api/api.go b/pkg/api/api.go index 876b6fa8c9a..799fd1fd982 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -166,9 +166,9 @@ func (a *API) RegisterAlertmanager(am *alertmanager.MultitenantAlertmanager, tar } // RegisterAPI registers the standard endpoints associated with a running Cortex. -func (a *API) RegisterAPI(cfg interface{}) { +func (a *API) RegisterAPI(httpPathPrefix string, cfg interface{}) { a.RegisterRoute("/config", configHandler(cfg), false) - a.RegisterRoute("/", http.HandlerFunc(indexHandler), false) + a.RegisterRoute("/", indexHandler(httpPathPrefix), false) } // RegisterDistributor registers the endpoints associated with the distributor. diff --git a/pkg/api/handlers.go b/pkg/api/handlers.go index ffbcad8e6b4..5fc097291f8 100644 --- a/pkg/api/handlers.go +++ b/pkg/api/handlers.go @@ -1,7 +1,9 @@ package api import ( + "html/template" "net/http" + "path" "github.com/go-kit/kit/log/level" "gopkg.in/yaml.v2" @@ -10,7 +12,7 @@ import ( ) // TODO: Update this content to be a template that is dynamic based on how Cortex is run. -const indexPageContent = ` +var indexPageContent = template.Must(template.New("main").Parse(` @@ -21,29 +23,39 @@ const indexPageContent = `

Cortex

Admin Endpoints:

Dangerous:

-` +`)) -func indexHandler(w http.ResponseWriter, _ *http.Request) { - if _, err := w.Write([]byte(indexPageContent)); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return +type indexPageInput struct { + pathPrefix string +} + +func (i indexPageInput) AddPathPrefix(p string) string { + return path.Join(i.pathPrefix, p) +} + +func indexHandler(httpPathPrefix string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + err := indexPageContent.Execute(w, indexPageInput{pathPrefix: httpPathPrefix}) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } } } diff --git a/pkg/api/handlers_test.go b/pkg/api/handlers_test.go new file mode 100644 index 00000000000..66ab282987d --- /dev/null +++ b/pkg/api/handlers_test.go @@ -0,0 +1,31 @@ +package api + +import ( + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIndexHandlerPrefix(t *testing.T) { + for _, tc := range []struct { + prefix string + toBeFound string + }{ + {prefix: "", toBeFound: ""}, + {prefix: "/test", toBeFound: ""}, + // All the extra slashed are cleaned up in the result. + {prefix: "///test///", toBeFound: ""}, + } { + h := indexHandler(tc.prefix) + + req := httptest.NewRequest("GET", "/", nil) + resp := httptest.NewRecorder() + + h.ServeHTTP(resp, req) + + require.Equal(t, 200, resp.Code) + require.True(t, strings.Contains(resp.Body.String(), tc.toBeFound)) + } +} diff --git a/pkg/cortex/modules.go b/pkg/cortex/modules.go index 06e47697271..f1ceef6ac0f 100644 --- a/pkg/cortex/modules.go +++ b/pkg/cortex/modules.go @@ -80,7 +80,7 @@ func (t *Cortex) initAPI() (services.Service, error) { t.API = a - t.API.RegisterAPI(t.Cfg) + t.API.RegisterAPI(t.Cfg.Server.PathPrefix, t.Cfg) return nil, nil }