Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Here is what you need to get started.
```python
from fastapi import FastAPI
from fastapi_healthcheck import HealthCheckFactory, healthCheckRoute
from fastapi_healthcheck.model import HealthCheckModel
from fastapi_healthcheck_sqlalchemy import HealthCheckSQLAlchemy

app = FastAPI()
Expand All @@ -28,7 +29,7 @@ _healthChecks.add(HealthCheckSQLAlchemy(alias='postgres db', connectionUri=cs.va
# This will check external URI and validate the response that is returned.
# fastapi-healthcheck-uri
_healthChecks.add(HealthCheckUri(alias='reddit', connectionUri="https://www.reddit.com/r/aww.json", tags=('external', 'reddit', 'aww')))
app.add_api_route('/health', endpoint=healthCheckRoute(factory=_healthChecks))
app.add_api_route('/health', endpoint=healthCheckRoute(factory=_healthChecks), response_model=HealthCheckModel)

```

Expand Down
4 changes: 2 additions & 2 deletions fastapi_healthcheck/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


class HealthCheckStatusEnum(Enum):
HEALTHY = "Healthy"
UNHEALTHY = "Unhealthy"
HEALTHY = "healthy"
UNHEALTHY = "unhealthy"
12 changes: 7 additions & 5 deletions fastapi_healthcheck/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ def healthCheckRoute(factory: HealthCheckFactory) -> Callable:
When called, the endpoint method within, will be called and it will run the job bound to the factory.
The results will be parsed and sent back to the requestor via JSON.
"""

_factory = factory

def endpoint() -> JSONResponse:
res = _factory.check()
if res['status'] == HealthCheckStatusEnum.UNHEALTHY.value:
return JSONResponse(content=res, status_code=500)
return JSONResponse(content=res, status_code=200)
"""
Check health of API and associated services.
"""
res = _factory.check()
code = 500 if res["status"] == HealthCheckStatusEnum.UNHEALTHY.value else 200
return JSONResponse(content=res, status_code=code)

return endpoint