""" Health check router. GET /api/health """ import logging from datetime import datetime, timezone from fastapi import APIRouter from app.core.database import get_db from app.core.redis import get_redis logger = logging.getLogger(__name__) router = APIRouter() @router.get("/health") async def health_check(): """Check system health including MongoDB and Redis connectivity.""" services = {} # Check MongoDB try: db = get_db() await db.command("ping") services["mongodb"] = "connected" except Exception as e: logger.error("MongoDB health check failed: %s", str(e)) services["mongodb"] = "disconnected" # Check Redis try: redis = get_redis() await redis.ping() services["redis"] = "connected" except Exception as e: logger.error("Redis health check failed: %s", str(e)) services["redis"] = "disconnected" # Overall status all_connected = all(v == "connected" for v in services.values()) status = "healthy" if all_connected else "degraded" return { "status": status, "timestamp": datetime.now(timezone.utc).isoformat(), "services": services, }