Skip to content

Commit b6e4ef4

Browse files
committed
Adds health check endpoint
Adds a /health endpoint that checks MongoDB connectivity. It returns a 200 OK status when healthy and a 503 Service Unavailable status when unhealthy. Includes details about the server's status, version, start time, and MongoDB's status. Also documents the new endpoint in the README.
1 parent 6178e04 commit b6e4ef4

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,39 @@ curl -X POST https://your-quantux-backend.com/rest/user/token-exchange \
9292

9393
The external API must implement `GET /v3/user/me` that returns a user object with at least `id` and `username` fields.
9494

95+
## Health Check
96+
97+
The server provides a health check endpoint at `/health` that checks MongoDB connectivity:
98+
99+
- **Path**: `/health`
100+
- **Method**: `GET`
101+
- **Response**:
102+
- `200 OK` when healthy (MongoDB is reachable)
103+
- `503 Service Unavailable` when unhealthy (MongoDB is unreachable)
104+
105+
The response includes:
106+
107+
- `status`: "UP" or "DOWN"
108+
- `version`: Server version
109+
- `started`: Server start timestamp
110+
- `mongo.status`: "UP" or "DOWN"
111+
- `mongo.error`: Error message (only present when MongoDB is down)
112+
113+
Example healthy response:
114+
115+
```json
116+
{
117+
"status": "UP",
118+
"version": "4.5.6",
119+
"started": "2024-01-01T12:00:00",
120+
"mongo": {
121+
"status": "UP"
122+
}
123+
}
124+
```
125+
126+
There is also a simpler status endpoint at `/rest/status.json` that returns version and start time without checking dependencies.
127+
95128
## Mongo optimization
96129

97130
Start the `mongo` shell and run the following commands to set the correct mongo indexes

src/main/java/com/qux/MATC.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public void start() {
6666

6767
initTokenService(config);
6868
initStatus(router);
69+
initHealth(router);
6970
initUserRest(config, router);
7071
initAppRest(router, config);
7172
initCommandRest(router);
@@ -117,6 +118,36 @@ private void initStatus(Router router) {
117118
.encodePrettily()));
118119
}
119120

121+
private void initHealth(Router router) {
122+
router.route(HttpMethod.GET, "/health").handler(event -> {
123+
JsonObject health = new JsonObject()
124+
.put("status", "UP")
125+
.put("version", VERSION)
126+
.put("started", startedTime);
127+
128+
// Check MongoDB connectivity using a simple count operation
129+
// Using an empty query on a collection to test connectivity
130+
client.count("_health_check", new JsonObject(), ar -> {
131+
if (ar.succeeded()) {
132+
health.put("mongo", new JsonObject().put("status", "UP"));
133+
event.response()
134+
.setStatusCode(200)
135+
.putHeader("Content-Type", "application/json")
136+
.end(health.encodePrettily());
137+
} else {
138+
health.put("status", "DOWN");
139+
health.put("mongo", new JsonObject()
140+
.put("status", "DOWN")
141+
.put("error", ar.cause() != null ? ar.cause().getMessage() : "Unknown error"));
142+
event.response()
143+
.setStatusCode(503)
144+
.putHeader("Content-Type", "application/json")
145+
.end(health.encodePrettily());
146+
}
147+
});
148+
});
149+
}
150+
120151
private void initLibrary(Router router) {
121152

122153
LibraryRest libs = new LibraryRest(this.tokenService, client);

0 commit comments

Comments
 (0)