
Create a simple Todo API with FastAPI and SQLite with CRUD functionality, health check, error handling, and API documentation.
49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
import platform
|
|
import sys
|
|
from datetime import datetime
|
|
|
|
from fastapi import APIRouter, Depends, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.config import settings
|
|
from app.database.session import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get(
|
|
"/health",
|
|
summary="Check health status",
|
|
status_code=status.HTTP_200_OK,
|
|
tags=["health"]
|
|
)
|
|
def health_check(db: Session = Depends(get_db)):
|
|
"""
|
|
Check the health of the application.
|
|
|
|
Returns:
|
|
dict: A dictionary containing health status information.
|
|
"""
|
|
health_data = {
|
|
"status": "ok",
|
|
"timestamp": datetime.utcnow().isoformat(),
|
|
"app_name": settings.PROJECT_NAME,
|
|
"environment": {
|
|
"python_version": sys.version,
|
|
"platform": platform.platform(),
|
|
},
|
|
"database": {
|
|
"status": "connected",
|
|
"type": "sqlite",
|
|
}
|
|
}
|
|
|
|
try:
|
|
# Verify database connectivity
|
|
db.execute("SELECT 1")
|
|
except Exception as e:
|
|
health_data["status"] = "error"
|
|
health_data["database"]["status"] = "error"
|
|
health_data["database"]["error"] = str(e)
|
|
|
|
return health_data |