Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kludex/fastapi-health
Implement the Health Check API pattern on your FastAPI application! :rocket:
https://github.com/kludex/fastapi-health
fastapi healthcheck
Last synced: 4 days ago
JSON representation
Implement the Health Check API pattern on your FastAPI application! :rocket:
- Host: GitHub
- URL: https://github.com/kludex/fastapi-health
- Owner: Kludex
- License: mit
- Created: 2021-01-23T23:57:27.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-07-30T18:04:36.000Z (over 1 year ago)
- Last Synced: 2024-10-30T06:58:27.347Z (3 months ago)
- Topics: fastapi, healthcheck
- Language: Python
- Homepage: https://kludex.github.io/fastapi-health/
- Size: 363 KB
- Stars: 175
- Watchers: 6
- Forks: 13
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
FastAPI Health 🚑️
The goal of this package is to help you to implement the [Health Check API](https://microservices.io/patterns/observability/health-check-api.html) pattern.
## Installation
``` bash
pip install fastapi-health
```## Quick Start
Create the health check endpoint dynamically using different conditions. Each condition is a
callable, and you can even have dependencies inside of it:```python
from fastapi import FastAPI, Depends
from fastapi_health import healthdef get_session():
return Truedef is_database_online(session: bool = Depends(get_session)):
return sessionapp = FastAPI()
app.add_api_route("/health", health([is_database_online]))
```## Advanced Usage
The `health()` method receives the following parameters:
- `conditions`: A list of callables that represents the conditions of your API, it can return either `bool` or a `dict`.
- `success_handler`: An optional callable which receives the `conditions` results and returns a dictionary that will be the content response of a successful health call.
- `failure_handler`: An optional callable analogous to `success_handler` for failure scenarios.
- `success_status`: An integer that overwrites the default status (200) in case of success.
- `failure_status`: An integer that overwrites the default status (503) in case of failure.It's important to notice that you can have a _peculiar_ behavior in case of hybrid return statements (`bool` and `dict`) on the conditions.
For example:``` Python
from fastapi import FastAPI
from fastapi_health import healthdef pass_condition():
return {"database": "online"}def sick_condition():
return Falseapp = FastAPI()
app.add_api_route("/health", health([pass_condition, sick_condition]))
```This will generate a response composed by the status being 503 (default `failure_status`), because `sick_condition` returns `False`, and the JSON body `{"database": "online"}`. It's not wrong, or a bug. It's meant to be like this.
## License
This project is licensed under the terms of the MIT license.