
- Set up project structure and FastAPI app - Create database models and SQLAlchemy connection - Implement Alembic migration scripts - Add CRUD API endpoints for Todo items - Add health check endpoint - Set up validation, error handling, and middleware - Add comprehensive documentation in README.md
38 lines
973 B
Python
38 lines
973 B
Python
from fastapi import APIRouter, Depends, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.session import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/health", response_model=dict, status_code=status.HTTP_200_OK)
|
|
async def health_check(db: Session = Depends(get_db)):
|
|
"""
|
|
Check the health of the application.
|
|
|
|
Tests:
|
|
- API availability
|
|
- Database connection
|
|
"""
|
|
health_data = {
|
|
"status": "ok",
|
|
"api": "up",
|
|
"database": "up",
|
|
}
|
|
|
|
# Test database connection
|
|
try:
|
|
# Execute a simple query
|
|
db.execute("SELECT 1")
|
|
except Exception as e:
|
|
health_data["status"] = "error"
|
|
health_data["database"] = "down"
|
|
health_data["database_error"] = str(e)
|
|
|
|
# Overall status is only "ok" if all components are up
|
|
if health_data["status"] == "ok":
|
|
return health_data
|
|
|
|
# Return 503 Service Unavailable if any component is down
|
|
return health_data |