Add health endpoints
This commit is contained in:
		
							parent
							
								
									7e814f4332
								
							
						
					
					
						commit
						45f7c672ac
					
				| @ -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) | ||||
|  | ||||
							
								
								
									
										31
									
								
								src/xmpp_api/api/routers/v1/health.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/xmpp_api/api/routers/v1/health.py
									
									
									
									
									
										Normal 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, | ||||
|     ) | ||||
							
								
								
									
										19
									
								
								src/xmpp_api/api/types/health.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/xmpp_api/api/types/health.py
									
									
									
									
									
										Normal 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 | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user