Files
BanGUI/backend/app/routers/health.py
Lukas f6c3c02183 Refactor response handling and health check endpoints
- Enhance response model with additional fields and validation
- Update health and server router implementations
- Improve frontend type definitions and API integration
- Clean up documentation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-02 21:57:00 +02:00

44 lines
1.6 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
from app.models.response import HealthResponse
router: APIRouter = APIRouter(prefix="/api/v1/health", tags=["Health"])
@router.get("", summary="Application health check", response_model=HealthResponse)
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 :class:`~app.models.response.HealthResponse` when healthy,
HTTP 503 with :class:`~app.models.response.HealthResponse` when fail2ban is offline.
"""
if not server_status.online:
return JSONResponse(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
content=HealthResponse(status="unavailable", fail2ban="offline").model_dump(),
)
return JSONResponse(
status_code=status.HTTP_200_OK,
content=HealthResponse(status="ok", fail2ban="online").model_dump(),
)