24 lines
770 B
Python
24 lines
770 B
Python
from fastapi import APIRouter, status
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.core.deps import DbSession
|
|
from app.schemas.health import HealthResponse
|
|
from app.services.health import check_database
|
|
|
|
router = APIRouter(tags=["health"])
|
|
|
|
|
|
@router.get("/health", response_model=HealthResponse)
|
|
def health_check(db: DbSession) -> HealthResponse | JSONResponse:
|
|
connected = check_database(db)
|
|
payload = HealthResponse(
|
|
status="ok" if connected else "degraded",
|
|
service="desi-clans-api",
|
|
database="connected" if connected else "unavailable",
|
|
)
|
|
if not connected:
|
|
return JSONResponse(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
content=payload.model_dump(),
|
|
)
|
|
return payload
|