|
1 | 1 | """Fastapi app creation.""" |
2 | 2 |
|
3 | 3 |
|
4 | | -from typing import Dict, List, Optional, Tuple, Type, Union |
| 4 | +from typing import Awaitable, Callable, Dict, List, Optional, Tuple, Type, Union |
5 | 5 |
|
6 | 6 | import attr |
7 | 7 | from brotli_asgi import BrotliMiddleware |
@@ -67,6 +67,10 @@ class StacApi: |
67 | 67 | specified routes. This is useful |
68 | 68 | for applying custom auth requirements to routes defined elsewhere in |
69 | 69 | the application. |
| 70 | + health_check: |
| 71 | + A Callable which return application's `health` information. |
| 72 | + Defaults to `def health: return {"status": "UP"}` |
| 73 | +
|
70 | 74 | """ |
71 | 75 |
|
72 | 76 | settings: ApiSettings = attr.ib() |
@@ -128,6 +132,9 @@ class StacApi: |
128 | 132 | ) |
129 | 133 | ) |
130 | 134 | route_dependencies: List[Tuple[List[Scope], List[Depends]]] = attr.ib(default=[]) |
| 135 | + health_check: Union[Callable[[], Dict], Callable[[], Awaitable[Dict]]] = attr.ib( |
| 136 | + default=lambda: {"status": "UP"} |
| 137 | + ) |
131 | 138 |
|
132 | 139 | def get_extension(self, extension: Type[ApiExtension]) -> Optional[ApiExtension]: |
133 | 140 | """Get an extension. |
@@ -363,14 +370,44 @@ def register_core(self) -> None: |
363 | 370 |
|
364 | 371 | def add_health_check(self) -> None: |
365 | 372 | """Add a health check.""" |
366 | | - mgmt_router = APIRouter(prefix=self.app.state.router_prefix) |
367 | 373 |
|
368 | | - @mgmt_router.get("/_mgmt/ping") |
369 | 374 | async def ping(): |
370 | | - """Liveliness/readiness probe.""" |
| 375 | + """Liveliness probe.""" |
371 | 376 | return {"message": "PONG"} |
372 | 377 |
|
373 | | - self.app.include_router(mgmt_router, tags=["Liveliness/Readiness"]) |
| 378 | + self.app.router.add_api_route( |
| 379 | + name="Ping", |
| 380 | + path="/_mgmt/ping", |
| 381 | + response_model=Dict, |
| 382 | + responses={ |
| 383 | + 200: { |
| 384 | + "content": { |
| 385 | + MimeTypes.json.value: {}, |
| 386 | + }, |
| 387 | + }, |
| 388 | + }, |
| 389 | + response_class=self.response_class, |
| 390 | + methods=["GET"], |
| 391 | + endpoint=ping, |
| 392 | + tags=["Liveliness/Readiness"], |
| 393 | + ) |
| 394 | + |
| 395 | + self.app.router.add_api_route( |
| 396 | + name="Health", |
| 397 | + path="/_mgmt/health", |
| 398 | + response_model=Dict, |
| 399 | + responses={ |
| 400 | + 200: { |
| 401 | + "content": { |
| 402 | + MimeTypes.json.value: {}, |
| 403 | + }, |
| 404 | + }, |
| 405 | + }, |
| 406 | + response_class=self.response_class, |
| 407 | + methods=["GET"], |
| 408 | + endpoint=self.health_check, |
| 409 | + tags=["Liveliness/Readiness"], |
| 410 | + ) |
374 | 411 |
|
375 | 412 | def add_route_dependencies( |
376 | 413 | self, scopes: List[Scope], dependencies: List[Depends] |
|
0 commit comments