- Add TrustedHostMiddleware for Host header validation - Add CORSMiddleware with configurable origins - Add rate limiting with RateLimitExceeded handler - Add custom middleware for security headers (HSTS, X-Frame-Options, etc.) - Add /health/db endpoint that checks database connectivity - Mark health endpoints as rate limit exempt - Fix linting issues in migration file (Rule 3 - Blocking)
44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
"""Health check endpoints."""
|
|
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy import text
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from backend.app.api.deps import get_db
|
|
from backend.app.core.security import limiter
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("")
|
|
@limiter.exempt
|
|
async def health_check() -> dict[str, str]:
|
|
"""Basic health check endpoint."""
|
|
return {"status": "healthy"}
|
|
|
|
|
|
@router.get("/ready")
|
|
@limiter.exempt
|
|
async def readiness_check() -> dict[str, str]:
|
|
"""Readiness check endpoint."""
|
|
return {"status": "ready"}
|
|
|
|
|
|
@router.get("/db")
|
|
@limiter.exempt
|
|
async def database_health_check(
|
|
db: AsyncSession = Depends(get_db),
|
|
) -> dict[str, Any]:
|
|
"""Health check that verifies database connectivity.
|
|
|
|
Returns:
|
|
Status indicating healthy/unhealthy and database connection state.
|
|
"""
|
|
try:
|
|
result = await db.execute(text("SELECT 1"))
|
|
result.scalar()
|
|
return {"status": "healthy", "database": "connected"}
|
|
except Exception as e:
|
|
return {"status": "unhealthy", "database": "error", "detail": str(e)}
|