"""Health check router.""" from fastapi import APIRouter, Depends, Request from sqlalchemy import text from sqlalchemy.ext.asyncio import AsyncSession from app.api.deps import get_current_admin, get_db from app.core.redis import check_redis_health router = APIRouter(prefix="/health", tags=["health"]) @router.get("") async def health_check(request: Request, db: AsyncSession = Depends(get_db)): """Basic health check.""" db_ok = False try: await db.execute(text("SELECT 1")) db_ok = True except Exception: db_ok = False redis_ok = await check_redis_health() status_code = "ok" if db_ok and redis_ok else "degraded" response = { "status": status_code, "service": "rss-platform", "db": "ok" if db_ok else "error", "redis": "ok" if redis_ok else "error", } warnings = getattr(request.app.state, "startup_warnings", None) if warnings: response["warnings"] = warnings return response @router.get("/db", dependencies=[Depends(get_current_admin)]) async def db_health(db: AsyncSession = Depends(get_db)): """Database health check.""" try: await db.execute(text("SELECT 1")) return {"status": "ok", "component": "database"} except Exception as exc: return {"status": "error", "component": "database", "detail": str(exc)} @router.get("/redis", dependencies=[Depends(get_current_admin)]) async def redis_health(): """Redis health check.""" ok = await check_redis_health() return {"status": "ok" if ok else "error", "component": "redis"}