46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
import os
|
|
import time
|
|
from typing import Any, Dict
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.session import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/")
|
|
async def health_check(db: Session = Depends(get_db)) -> Dict[str, Any]:
|
|
"""
|
|
Health check endpoint for the Inventory Management System API.
|
|
Checks database connectivity and returns status.
|
|
"""
|
|
start_time = time.time()
|
|
health_data = {
|
|
"status": "healthy",
|
|
"timestamp": start_time,
|
|
"components": {
|
|
"database": {"status": "unhealthy", "details": "Could not connect to database"},
|
|
"api": {"status": "healthy", "details": "API is operational"},
|
|
},
|
|
"environment": os.environ.get("ENVIRONMENT", "development"),
|
|
}
|
|
|
|
# Check database connectivity
|
|
try:
|
|
# Try to execute a simple query to check database connectivity
|
|
db.execute("SELECT 1")
|
|
health_data["components"]["database"] = {
|
|
"status": "healthy",
|
|
"details": "Database connection successful",
|
|
}
|
|
except Exception as e:
|
|
health_data["status"] = "unhealthy"
|
|
health_data["components"]["database"] = {"status": "unhealthy", "details": str(e)}
|
|
|
|
# Calculate response time
|
|
health_data["response_time_ms"] = round((time.time() - start_time) * 1000, 2)
|
|
|
|
return health_data
|