from fastapi import APIRouter, HTTPException from sqlmodel import Session, select from core.cache import auth_cache, cache from core.database import engine from core.settings import DATABASE_ENGINE, REDIS_ENABLED, REDIS_PREFIX, REDIS_URL from models.bot import BotInstance router = APIRouter() @router.get("/api/health") def get_health(): try: with Session(engine) as session: session.exec(select(BotInstance).limit(1)).first() return {"status": "ok", "database": DATABASE_ENGINE} except Exception as exc: raise HTTPException(status_code=503, detail=f"database check failed: {exc}") from exc @router.get("/api/health/cache") def get_cache_health(): redis_url = str(REDIS_URL or "").strip() configured = bool(REDIS_ENABLED and redis_url) client_enabled = bool(getattr(cache, "enabled", False)) reachable = bool(cache.ping()) if client_enabled else False status = "ok" if configured and not reachable: status = "degraded" return { "status": status, "cache": { "configured": configured, "enabled": client_enabled, "reachable": reachable, "prefix": REDIS_PREFIX, "status": str(getattr(cache, "status", "") or ""), "detail": str(getattr(cache, "status_detail", "") or ""), }, "auth_store": { "enabled": bool(getattr(auth_cache, "enabled", False)), "status": str(getattr(auth_cache, "status", "") or ""), "detail": str(getattr(auth_cache, "status_detail", "") or ""), }, }