-
Notifications
You must be signed in to change notification settings - Fork 52
feat(backend): add /health endpoint #1529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
packages/backend/src/health/controllers/health.controller.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import { Status } from "@core/errors/status.codes"; | ||
| import { BaseDriver } from "@backend/__tests__/drivers/base.driver"; | ||
| import { | ||
| cleanupTestDb, | ||
| setupTestDb, | ||
| } from "@backend/__tests__/helpers/mock.db.setup"; | ||
| import mongoService from "@backend/common/services/mongo.service"; | ||
|
|
||
| describe("HealthController", () => { | ||
| const baseDriver = new BaseDriver(); | ||
|
|
||
| beforeAll(setupTestDb); | ||
| afterAll(cleanupTestDb); | ||
|
|
||
| describe("check", () => { | ||
| it("should return 200 OK with status ok and timestamp", async () => { | ||
| const response = await baseDriver | ||
| .getServer() | ||
| .get("/api/health") | ||
| .expect(Status.OK); | ||
|
|
||
| expect(response.body).toEqual({ | ||
| status: "ok", | ||
| timestamp: expect.any(String), | ||
| }); | ||
|
|
||
| // Verify timestamp is a valid ISO string | ||
| const timestamp = new Date(response.body.timestamp); | ||
| expect(timestamp.toISOString()).toBe(response.body.timestamp); | ||
| }); | ||
|
|
||
| it("should return a recent timestamp", async () => { | ||
| const beforeRequest = new Date(); | ||
| const response = await baseDriver | ||
| .getServer() | ||
| .get("/api/health") | ||
| .expect(Status.OK); | ||
| const afterRequest = new Date(); | ||
|
|
||
| const responseTimestamp = new Date(response.body.timestamp); | ||
|
|
||
| // Timestamp should be between before and after request time | ||
| expect(responseTimestamp.getTime()).toBeGreaterThanOrEqual( | ||
| beforeRequest.getTime() - 1000, // Allow 1 second tolerance | ||
| ); | ||
| expect(responseTimestamp.getTime()).toBeLessThanOrEqual( | ||
| afterRequest.getTime() + 1000, // Allow 1 second tolerance | ||
| ); | ||
| }); | ||
|
|
||
| it("should not require authentication", async () => { | ||
| // Health endpoint should be accessible without session | ||
| const response = await baseDriver | ||
| .getServer() | ||
| .get("/api/health") | ||
| .expect(Status.OK); | ||
|
|
||
| expect(response.body.status).toBe("ok"); | ||
| }); | ||
|
|
||
| it("should be accessible even with invalid session", async () => { | ||
| // Health endpoint should work regardless of session validity | ||
| const response = await baseDriver | ||
| .getServer() | ||
| .get("/api/health") | ||
| .set("Cookie", "session=invalid") | ||
| .expect(Status.OK); | ||
|
|
||
| expect(response.body.status).toBe("ok"); | ||
| }); | ||
|
|
||
| it("should verify database connectivity", async () => { | ||
| // When database is connected, endpoint should return successfully | ||
| const response = await baseDriver | ||
| .getServer() | ||
| .get("/api/health") | ||
| .expect(Status.OK); | ||
|
|
||
| expect(response.body).toEqual({ | ||
| status: "ok", | ||
| timestamp: expect.any(String), | ||
| }); | ||
| }); | ||
|
|
||
| it("should return 500 when database connectivity check fails", async () => { | ||
| const pingSpy = jest | ||
| .spyOn(Object.getPrototypeOf(mongoService.db.admin()), "ping") | ||
| .mockRejectedValue(new Error("database unavailable")); | ||
|
|
||
| try { | ||
| const response = await baseDriver | ||
| .getServer() | ||
| .get("/api/health") | ||
| .expect(Status.INTERNAL_SERVER); | ||
|
|
||
| expect(response.body).toEqual({ | ||
| status: "error", | ||
| timestamp: expect.any(String), | ||
| }); | ||
| } finally { | ||
| pingSpy.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| it("should return consistent response structure", async () => { | ||
| const response = await baseDriver | ||
| .getServer() | ||
| .get("/api/health") | ||
| .expect(Status.OK); | ||
|
|
||
| // Verify response has exactly the expected fields | ||
| expect(Object.keys(response.body)).toEqual(["status", "timestamp"]); | ||
| expect(typeof response.body.status).toBe("string"); | ||
| expect(typeof response.body.timestamp).toBe("string"); | ||
| expect(response.body.status).toBe("ok"); | ||
| }); | ||
|
|
||
| it("should handle multiple concurrent requests", async () => { | ||
| // Use fewer concurrent requests to avoid connection issues | ||
| const requests = Array.from({ length: 3 }, () => | ||
| baseDriver | ||
| .getServer() | ||
| .get("/api/health") | ||
| .expect(Status.OK) | ||
| .catch((error) => { | ||
| // Retry once on connection error | ||
| return baseDriver.getServer().get("/api/health").expect(Status.OK); | ||
| }), | ||
| ); | ||
|
|
||
| const responses = await Promise.all(requests); | ||
|
|
||
| // All requests should succeed | ||
| responses.forEach((response) => { | ||
| expect(response.body.status).toBe("ok"); | ||
| expect(response.body.timestamp).toBeDefined(); | ||
| }); | ||
|
|
||
| // Timestamps should be close to each other (within same second) | ||
| const timestamps = responses.map((r) => new Date(r.body.timestamp)); | ||
| const minTime = Math.min(...timestamps.map((t) => t.getTime())); | ||
| const maxTime = Math.max(...timestamps.map((t) => t.getTime())); | ||
| expect(maxTime - minTime).toBeLessThan(2000); // Within 2 seconds | ||
| }); | ||
|
|
||
| it("should return proper content-type header", async () => { | ||
| const response = await baseDriver | ||
| .getServer() | ||
| .get("/api/health") | ||
| .expect(Status.OK); | ||
|
|
||
| expect(response.headers["content-type"]).toMatch(/application\/json/); | ||
| }); | ||
| }); | ||
| }); |
45 changes: 45 additions & 0 deletions
45
packages/backend/src/health/controllers/health.controller.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { type Request, type Response } from "express"; | ||
| import { Status } from "@core/errors/status.codes"; | ||
| import { Logger } from "@core/logger/winston.logger"; | ||
| import mongoService from "@backend/common/services/mongo.service"; | ||
|
|
||
| interface HealthResponse { | ||
| status: "ok" | "error"; | ||
| timestamp: string; | ||
| } | ||
|
|
||
| const logger = Logger("app:health.controller"); | ||
|
|
||
| class HealthController { | ||
| /** | ||
| * GET /api/health | ||
| * Health check endpoint that verifies basic system connectivity | ||
| * | ||
| * @returns {Object} Health status with timestamp | ||
| * @returns {200} OK - Database is reachable | ||
| * @returns {500} Internal Server Error - Database is unreachable | ||
| */ | ||
| check = async ( | ||
| _req: Request<never, HealthResponse, never, never>, | ||
| res: Response<HealthResponse>, | ||
| ) => { | ||
| const timestamp = new Date().toISOString(); | ||
|
|
||
| try { | ||
| await mongoService.db.admin().ping(); | ||
|
|
||
| res.status(Status.OK).json({ | ||
| status: "ok", | ||
| timestamp, | ||
| }); | ||
| } catch (error) { | ||
| logger.error("Database connectivity check failed", error); | ||
| res.status(Status.INTERNAL_SERVER).json({ | ||
| status: "error", | ||
| timestamp, | ||
| }); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| export default new HealthController(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type express from "express"; | ||
| import { CommonRoutesConfig } from "@backend/common/common.routes.config"; | ||
| import healthController from "./controllers/health.controller"; | ||
|
|
||
| /** | ||
| * Health Routes Configuration | ||
| * | ||
| * Provides health check endpoint for monitoring system status. | ||
| * This endpoint does not require authentication as it's used by | ||
| * load balancers, monitoring tools, and orchestration systems. | ||
| */ | ||
| export class HealthRoutes extends CommonRoutesConfig { | ||
| constructor(app: express.Application) { | ||
| super(app, "HealthRoutes"); | ||
| } | ||
|
|
||
| configureRoutes(): express.Application { | ||
| /** | ||
| * GET /api/health | ||
| * Health check endpoint that verifies basic system connectivity | ||
| * | ||
| * @returns {Object} Health status with timestamp | ||
| * @returns {200} OK - Database is reachable | ||
| * @returns {500} Internal Server Error - Database is unreachable | ||
| */ | ||
| this.app.route(`/api/health`).get(healthController.check); | ||
|
|
||
| return this.app; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.