Files
hms-licht-ton/backend/app/routers/health.py
T

31 lines
818 B
Python
Raw Normal View History

"""Health check endpoint."""
from fastapi import APIRouter
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import Depends
from app.cache import ping_cache
from app.database import get_db
router = APIRouter()
@router.get("/health")
async def health_check(db: AsyncSession = Depends(get_db)) -> dict:
"""Return service health status including DB and Redis connectivity."""
db_ok = False
try:
result = await db.execute(text("SELECT 1"))
db_ok = result.scalar() == 1
except Exception:
db_ok = False
redis_ok = await ping_cache()
return {
"status": "ok" if (db_ok and redis_ok) else "degraded",
"db": "connected" if db_ok else "disconnected",
"redis": "connected" if redis_ok else "disconnected",
}