Files
service_manager/backend/app/api/v1/endpoints/health.py
2026-01-12 08:17:17 -07:00

88 lines
2.3 KiB
Python

"""
Health Check Endpoints - ServiceManagerWeb
Endpoints para health checks y monitoring
"""
from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession
import structlog
from app.core.database import get_db, check_database_health
from app.core.config import get_settings
router = APIRouter()
logger = structlog.get_logger(__name__)
settings = get_settings()
@router.get("/health")
async def health_check():
"""
Basic health check endpoint.
Returns basic service information and status.
"""
return {
"status": "healthy",
"service": "ServiceManagerWeb API",
"version": settings.API_VERSION,
"environment": settings.ENVIRONMENT
}
@router.get("/health/detailed")
async def detailed_health_check(db: AsyncSession = Depends(get_db)):
"""
Detailed health check with database connectivity.
Checks database connection and returns detailed status.
"""
# Check database
db_healthy = await check_database_health()
# TODO: Add Redis health check
# TODO: Add Celery health check
overall_status = "healthy" if db_healthy else "unhealthy"
status_code = status.HTTP_200_OK if db_healthy else status.HTTP_503_SERVICE_UNAVAILABLE
health_data = {
"status": overall_status,
"service": "ServiceManagerWeb API",
"version": settings.API_VERSION,
"environment": settings.ENVIRONMENT,
"checks": {
"database": "healthy" if db_healthy else "unhealthy",
"redis": "not_implemented",
"celery": "not_implemented"
}
}
if not db_healthy:
logger.error("Health check failed - database unhealthy")
return health_data
@router.get("/readiness")
async def readiness_check():
"""
Kubernetes readiness probe endpoint.
Returns 200 if service is ready to accept traffic.
"""
# For now, just return ready
# In production, this might check for startup completion,
# database migrations, etc.
return {"status": "ready"}
@router.get("/liveness")
async def liveness_check():
"""
Kubernetes liveness probe endpoint.
Returns 200 if service is alive and should not be restarted.
"""
return {"status": "alive"}