feat: project skeleton
- infra (k8s, kind, helm, docker) backbone is implemented - security: implementation + unit tests are done
This commit is contained in:
0
app/api/__init__.py
Normal file
0
app/api/__init__.py
Normal file
0
app/api/v1/__init__.py
Normal file
0
app/api/v1/__init__.py
Normal file
46
app/api/v1/health.py
Normal file
46
app/api/v1/health.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""Health check endpoints."""
|
||||
|
||||
from fastapi import APIRouter, Response, status
|
||||
|
||||
from app.db import db, redis_client
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/healthz")
|
||||
async def healthz() -> dict[str, str]:
|
||||
"""Liveness probe - returns 200 if the service is running."""
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.get("/readyz")
|
||||
async def readyz(response: Response) -> dict[str, str | dict[str, bool]]:
|
||||
"""
|
||||
Readiness probe - checks database and Redis connectivity.
|
||||
- Check Postgres status
|
||||
- Check Redis status
|
||||
- Return overall healthiness
|
||||
"""
|
||||
checks = {
|
||||
"postgres": False,
|
||||
"redis": False,
|
||||
}
|
||||
|
||||
try:
|
||||
if db.pool:
|
||||
async with db.connection() as conn:
|
||||
await conn.fetchval("SELECT 1")
|
||||
checks["postgres"] = True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
checks["redis"] = await redis_client.ping()
|
||||
|
||||
all_healthy = all(checks.values())
|
||||
if not all_healthy:
|
||||
response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
|
||||
|
||||
return {
|
||||
"status": "ok" if all_healthy else "degraded",
|
||||
"checks": checks,
|
||||
}
|
||||
Reference in New Issue
Block a user