diff --git a/controllers/health_controller.go b/controllers/health_controller.go index 7106285..7d53f91 100644 --- a/controllers/health_controller.go +++ b/controllers/health_controller.go @@ -24,16 +24,18 @@ type ReadinessResponse struct { Targets map[string]TargetHealth `json:"targets"` } -// RegisterHealthRoutes mounts /healthz and /readyz probes +// RegisterHealthRoutes mounts /health/live, /health/ready, and backward-compatible /healthz, /readyz probes func RegisterHealthRoutes(app *fiber.App, db *gorm.DB) { - app.Get("/healthz", func(c *fiber.Ctx) error { + liveHandler := func(c *fiber.Ctx) error { return c.JSON(fiber.Map{ "status": "UP", "timestamp": time.Now().UTC(), }) - }) + } + app.Get("/health/live", liveHandler) + app.Get("/healthz", liveHandler) - app.Get("/readyz", func(c *fiber.Ctx) error { + readyHandler := func(c *fiber.Ctx) error { ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() @@ -116,5 +118,7 @@ func RegisterHealthRoutes(app *fiber.App, db *gorm.DB) { Timestamp: time.Now().UTC(), Targets: targets, }) - }) + } + app.Get("/health/ready", readyHandler) + app.Get("/readyz", readyHandler) } diff --git a/main_test.go b/main_test.go index d4d00ba..356c4b8 100644 --- a/main_test.go +++ b/main_test.go @@ -86,11 +86,24 @@ func TestHealthRoutes(t *testing.T) { controllers.RegisterHealthRoutes(app, db) - req, _ := http.NewRequest(http.MethodGet, "/healthz", nil) - resp, err := app.Test(req, -1) - assert.NoError(t, err) - assert.Equal(t, 200, resp.StatusCode) + // Liveness tests (/health/live and /healthz) + for _, path := range []string{"/health/live", "/healthz"} { + req, _ := http.NewRequest(http.MethodGet, path, nil) + resp, err := app.Test(req, -1) + assert.NoError(t, err, path) + assert.Equal(t, 200, resp.StatusCode, path) - body, _ := io.ReadAll(resp.Body) - assert.Contains(t, string(body), `"status":"UP"`) + body, _ := io.ReadAll(resp.Body) + assert.Contains(t, string(body), `"status":"UP"`, path) + } + + // Readiness tests (/health/ready and /readyz) + for _, path := range []string{"/health/ready", "/readyz"} { + req, _ := http.NewRequest(http.MethodGet, path, nil) + resp, err := app.Test(req, -1) + assert.NoError(t, err, path) + // Since in-memory SQLite doesn't have dbresolver write/read targets, status is 503 DEGRADED or 200 UP + body, _ := io.ReadAll(resp.Body) + assert.Contains(t, string(body), `"targets"`, path) + } }