40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.orm import Session
|
|
from app.database import get_db
|
|
import httpx
|
|
from app.config import COINCAP_API_BASE_URL, COINCAP_API_KEY
|
|
|
|
router = APIRouter(tags=["Health"])
|
|
|
|
@router.get("/health", summary="Check service health")
|
|
async def health_check(db: Session = Depends(get_db)):
|
|
"""
|
|
Health check endpoint that verifies:
|
|
- Database connection
|
|
- External API connectivity
|
|
"""
|
|
# Check database connection
|
|
try:
|
|
db.execute("SELECT 1")
|
|
db_status = "healthy"
|
|
except Exception as e:
|
|
db_status = f"unhealthy: {str(e)}"
|
|
|
|
# Check CoinCap API connection
|
|
try:
|
|
async with httpx.AsyncClient() as client:
|
|
response = await client.get(
|
|
f"{COINCAP_API_BASE_URL}/assets",
|
|
params={"apiKey": COINCAP_API_KEY, "limit": 1}
|
|
)
|
|
response.raise_for_status()
|
|
api_status = "healthy"
|
|
except Exception as e:
|
|
api_status = f"unhealthy: {str(e)}"
|
|
|
|
return {
|
|
"status": "ok" if db_status == "healthy" and api_status == "healthy" else "degraded",
|
|
"database": db_status,
|
|
"coincap_api": api_status,
|
|
"version": "0.1.0"
|
|
} |