Skip to content

Commit 6788a2c

Browse files
authored
Merge pull request #795 from seapagan/feature/heartbeat-endpoint
Add heartbeat endpoint and tests
2 parents 9c23bea + a11e4f7 commit 6788a2c

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ following advantages to starting your own from scratch :
129129
when new users register. The content is set by a template (currently a basic
130130
placeholder). This email has a link for the user to confirm their email
131131
address - until this is done, the user cannot user the API.
132+
- A lightweight `/heartbeat` endpoint for uptime/health checks.
132133
- Docker and Compose file set up to develop and test this API using Docker
133134

134135
**This template is still in very active development and probably not yet ready

app/resources/heartbeat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Routes for service health checks."""
2+
3+
from fastapi import APIRouter
4+
5+
router = APIRouter(tags=["Health"])
6+
7+
8+
@router.get("/heartbeat", summary="Simple uptime probe")
9+
def heartbeat() -> dict[str, str]:
10+
"""Return a minimal response to show the service is up."""
11+
return {"status": "ok"}

app/resources/routes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
from fastapi import APIRouter
44

55
from app.config.settings import get_settings
6-
from app.resources import api_key, auth, home, user
6+
from app.resources import api_key, auth, heartbeat, home, user
77

88
api_router = APIRouter(prefix=get_settings().api_root)
99

1010
api_router.include_router(user.router)
1111
api_router.include_router(auth.router)
1212
api_router.include_router(api_key.router)
13+
api_router.include_router(heartbeat.router)
1314

1415
if not get_settings().no_root_route:
1516
api_router.include_router(home.router)

docs/reference/api.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,24 @@ Delete another user's API key.
824824

825825
---
826826

827+
## Health Endpoints
828+
829+
### Heartbeat
830+
831+
**Endpoint:** `GET /heartbeat`
832+
833+
Returns a minimal response to confirm the service is up.
834+
835+
**Response:** `200 OK`
836+
837+
```json
838+
{
839+
"status": "ok"
840+
}
841+
```
842+
843+
---
844+
827845
## Authentication Methods
828846

829847
The API supports two authentication methods:

tests/integration/test_home_routes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ async def test_favicon(self, client) -> None:
3131
assert response.headers["content-type"] == "image/vnd.microsoft.icon"
3232
# Ensure we actually got some content
3333
assert len(response.content) > 0
34+
35+
@pytest.mark.asyncio
36+
async def test_heartbeat(self, client) -> None:
37+
"""Test the heartbeat route returns a simple OK payload."""
38+
response = await client.get("/heartbeat")
39+
assert response.status_code == status.HTTP_200_OK
40+
assert response.json() == {"status": "ok"}

0 commit comments

Comments
 (0)