- All backend routers moved to /api/v1/ prefix
- Frontend BASE_URL updated to /api/v1
- Setup redirect middleware updated to redirect to /api/v1/setup
- Health router path fixed: prefix=/api/v1/health, @router.get('')
- conftest.py: set server_status=online for test fixture
- Created Docs/API_VERSIONING.md with deprecation policy
- Updated Docs/Backend-Development.md with versioning section
- Updated Instructions.md curl examples
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
"""Health check router.
|
|
|
|
A lightweight ``GET /api/health`` endpoint that verifies the application
|
|
is running and can serve requests. Also reports the cached fail2ban liveness
|
|
state so monitoring tools and Docker health checks can observe daemon status
|
|
without probing the socket directly.
|
|
"""
|
|
|
|
from fastapi import APIRouter, status
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.dependencies import ServerStatusDep
|
|
|
|
router: APIRouter = APIRouter(prefix="/api/v1/health", tags=["Health"])
|
|
|
|
|
|
@router.get("", summary="Application health check")
|
|
async def health_check(server_status: ServerStatusDep) -> JSONResponse:
|
|
"""Return application and fail2ban status.
|
|
|
|
Returns HTTP 200 if fail2ban is online, HTTP 503 if offline.
|
|
Docker health checks interpret 503 as unhealthy and restart the container
|
|
if fail2ban remains unreachable, ensuring the backend only runs when
|
|
fail2ban is available.
|
|
|
|
Args:
|
|
server_status: Injected cached server status snapshot.
|
|
|
|
Returns:
|
|
HTTP 200 with ``{"status": "ok", "fail2ban": "online"}`` if healthy,
|
|
or HTTP 503 with ``{"status": "unavailable", "fail2ban": "offline"}``
|
|
if fail2ban is unreachable.
|
|
"""
|
|
if not server_status.online:
|
|
return JSONResponse(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
content={"status": "unavailable", "fail2ban": "offline"},
|
|
)
|
|
|
|
return JSONResponse(
|
|
status_code=status.HTTP_200_OK,
|
|
content={"status": "ok", "fail2ban": "online"},
|
|
)
|