"""Health endpoint — also the smoke test for database connectivity.""" from fastapi import APIRouter, Depends from sqlalchemy import text from sqlalchemy.orm import Session from app.core.config import get_settings from app.db.session import get_db from app.schemas.health import HealthResponse router = APIRouter(tags=["health"]) @router.get("/health", response_model=HealthResponse, summary="Liveness and database probe") def health(db: Session = Depends(get_db)) -> HealthResponse: """Report process liveness and whether TiDB answered a trivial query. A database failure is reported in the body instead of raising, so that the endpoint stays useful for diagnosing an unreachable database. """ settings = get_settings() try: db.execute(text("SELECT 1")) database = "ok" except Exception: # noqa: BLE001 - the failure mode is the payload database = "unavailable" return HealthResponse( status="ok" if database == "ok" else "degraded", app=settings.app_name, database=database, database_target=settings.safe_database_url, )