Spaces:
Running
Running
Add safe backend database readiness endpoint
Browse files- app/api/health.py +50 -0
app/api/health.py
CHANGED
|
@@ -5,6 +5,7 @@ import shutil
|
|
| 5 |
import time
|
| 6 |
|
| 7 |
from fastapi import APIRouter, Request
|
|
|
|
| 8 |
|
| 9 |
from app.core.response import SuccessResponse
|
| 10 |
|
|
@@ -35,3 +36,52 @@ router.add_api_route("/health", _health, methods=["GET"], response_model=Success
|
|
| 35 |
router.add_api_route(
|
| 36 |
"/v1/health", _health, methods=["GET"], response_model=SuccessResponse, include_in_schema=False
|
| 37 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import time
|
| 6 |
|
| 7 |
from fastapi import APIRouter, Request
|
| 8 |
+
from fastapi.responses import JSONResponse
|
| 9 |
|
| 10 |
from app.core.response import SuccessResponse
|
| 11 |
|
|
|
|
| 36 |
router.add_api_route(
|
| 37 |
"/v1/health", _health, methods=["GET"], response_model=SuccessResponse, include_in_schema=False
|
| 38 |
)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
async def _readiness(request: Request) -> JSONResponse:
|
| 42 |
+
"""Report database/schema readiness without disclosing credentials or DSNs."""
|
| 43 |
+
container = request.app.state.container
|
| 44 |
+
database = container.security_database
|
| 45 |
+
try:
|
| 46 |
+
missing = await database.missing_schema_objects()
|
| 47 |
+
except Exception:
|
| 48 |
+
return JSONResponse(
|
| 49 |
+
status_code=503,
|
| 50 |
+
content={"status": "not_ready", "database": "unavailable", "schema_ready": False},
|
| 51 |
+
)
|
| 52 |
+
if missing:
|
| 53 |
+
return JSONResponse(
|
| 54 |
+
status_code=503,
|
| 55 |
+
content={
|
| 56 |
+
"status": "not_ready",
|
| 57 |
+
"database": "connected",
|
| 58 |
+
"schema_ready": False,
|
| 59 |
+
"missing_schema_objects": missing,
|
| 60 |
+
},
|
| 61 |
+
)
|
| 62 |
+
try:
|
| 63 |
+
await database.verify_execution_boundary(
|
| 64 |
+
expected_role=container.settings.security_database_role,
|
| 65 |
+
enforce_rls=container.settings.security_enforce_rls,
|
| 66 |
+
)
|
| 67 |
+
except Exception:
|
| 68 |
+
return JSONResponse(
|
| 69 |
+
status_code=503,
|
| 70 |
+
content={
|
| 71 |
+
"status": "not_ready",
|
| 72 |
+
"database": "connected",
|
| 73 |
+
"schema_ready": True,
|
| 74 |
+
"execution_role_ready": False,
|
| 75 |
+
},
|
| 76 |
+
)
|
| 77 |
+
return JSONResponse(
|
| 78 |
+
status_code=200,
|
| 79 |
+
content={
|
| 80 |
+
"status": "ready",
|
| 81 |
+
"database": "connected",
|
| 82 |
+
"schema_ready": True,
|
| 83 |
+
"execution_role_ready": True,
|
| 84 |
+
},
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
router.add_api_route("/ready", _readiness, methods=["GET"], include_in_schema=False)
|