Add health endpoints

This commit is contained in:
PapaTutuWawa 2025-04-21 13:20:20 +02:00
parent 7e814f4332
commit 45f7c672ac
3 changed files with 52 additions and 1 deletions

View File

@ -1,8 +1,9 @@
from fastapi import APIRouter
from xmpp_api.api.routers.v1 import bot, user
from xmpp_api.api.routers.v1 import bot, user, health
router = APIRouter(prefix="/api/v1")
router.include_router(bot.router)
router.include_router(user.router)
router.include_router(health.router)

View File

@ -0,0 +1,31 @@
from fastapi import APIRouter, Response
from xmpp_api.api.types.health import HealthResponse, ReadinessResponse
from xmpp_api.xmpp.component import XmppApiComponentDep
router = APIRouter(prefix="/health")
@router.get("/readiness")
def get_is_ready(
component: XmppApiComponentDep, response: Response
) -> ReadinessResponse:
component_ready = component.is_connected()
response.status_code = 200 if component_ready else 503
return ReadinessResponse(
component=component_ready,
)
@router.get("/healthy")
def get_is_healthy(
component: XmppApiComponentDep, response: Response
) -> HealthResponse:
component_ready = component.is_connected()
response.status_code = 200 if component_ready else 503
return HealthResponse(
component=component_ready,
)

View File

@ -0,0 +1,19 @@
from pydantic import BaseModel
class ReadinessResponse(BaseModel):
"""
API response for the readiness endpoint
"""
# Is the component connected
component: bool
class HealthResponse(BaseModel):
"""
API response for the health endpoint
"""
# Is the component healthy
component: bool