Frontend service: add empty health route (#112212)
* add empty health route to frontend-service * add OK response * add test * handle error case
This commit is contained in:
@@ -138,6 +138,16 @@ func (s *frontendService) registerRoutes(m *web.Mux) {
|
||||
// them so we can get logs for them
|
||||
s.routeGet(m, "/public/*", http.NotFound)
|
||||
|
||||
// Empty health check endpoint to allow k8s and other orchestrators to check if the server is alive
|
||||
// Useful to have a separate route for this for logging and metrics purposes
|
||||
s.routeGet(m, "/-/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
if _, err := w.Write([]byte("OK")); err != nil {
|
||||
s.log.Error("failed to write health check response", "error", err)
|
||||
}
|
||||
})
|
||||
|
||||
// All other requests return index.html
|
||||
s.routeGet(m, "/*", s.index.HandleRequest)
|
||||
}
|
||||
|
||||
@@ -120,6 +120,16 @@ func TestFrontendService_Routes(t *testing.T) {
|
||||
|
||||
assert.Contains(t, recorder.Body.String(), "\nshrimp_count 1\n")
|
||||
})
|
||||
|
||||
t.Run("should return health status correctly", func(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/-/health", nil)
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
mux.ServeHTTP(recorder, req)
|
||||
|
||||
assert.Equal(t, 200, recorder.Code)
|
||||
assert.Equal(t, "OK", strings.TrimSpace(recorder.Body.String()))
|
||||
})
|
||||
}
|
||||
|
||||
func TestFrontendService_Middleware(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user