dashboard-nanobot/backend/api/health_router.py

47 lines
1.6 KiB
Python
Raw Permalink Normal View History

2026-04-04 16:29:37 +00:00
from fastapi import APIRouter, HTTPException
from sqlmodel import Session, select
2026-04-13 12:07:07 +00:00
from core.cache import auth_cache, cache
2026-04-04 16:29:37 +00:00
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,
2026-04-13 12:07:07 +00:00
"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 ""),
2026-04-04 16:29:37 +00:00
},
}