|
9 | 9 |
|
10 | 10 | from __future__ import annotations |
11 | 11 |
|
12 | | -from fastapi import APIRouter, Request |
13 | | -from fastapi.responses import JSONResponse |
| 12 | +from fastapi import APIRouter, Request, Response |
14 | 13 |
|
15 | 14 | from middleware.openapi import PUBLIC_SECURITY |
| 15 | +from schemas.dto.responses.common import HealthResponse |
16 | 16 |
|
17 | 17 | router = APIRouter(tags=["System"]) |
18 | 18 |
|
|
22 | 22 | openapi_extra=PUBLIC_SECURITY, |
23 | 23 | operation_id="healthCheck", |
24 | 24 | summary="Health Check", |
| 25 | + responses={ |
| 26 | + 503: { |
| 27 | + "description": "Unhealthy — MongoDB is unreachable", |
| 28 | + "model": HealthResponse, |
| 29 | + } |
| 30 | + }, |
25 | 31 | ) |
26 | | -async def health_check(request: Request) -> JSONResponse: |
| 32 | +async def health_check(request: Request, response: Response) -> HealthResponse: |
27 | 33 | """Check the health of the application and its dependencies. |
28 | 34 |
|
29 | 35 | Pings MongoDB and Redis to determine overall system status: |
@@ -63,12 +69,10 @@ async def health_check(request: Request) -> JSONResponse: |
63 | 69 |
|
64 | 70 | settings = getattr(request.app.state, "settings", None) |
65 | 71 |
|
66 | | - status_code = 503 if overall == "unhealthy" else 200 |
67 | | - return JSONResponse( |
68 | | - status_code=status_code, |
69 | | - content={ |
70 | | - "status": overall, |
71 | | - "version": settings.app_version if settings else "dev", |
72 | | - "checks": checks, |
73 | | - }, |
| 72 | + if overall == "unhealthy": |
| 73 | + response.status_code = 503 |
| 74 | + return HealthResponse( |
| 75 | + status=overall, |
| 76 | + version=settings.app_version if settings else "dev", |
| 77 | + checks=checks, |
74 | 78 | ) |
0 commit comments