Skip to content

Commit 511b673

Browse files
committed
add health api endpoint to check health of API
1 parent 76d0e8f commit 511b673

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

api.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const dkimRoutes = require('./lib/api/dkim');
4747
const certsRoutes = require('./lib/api/certs');
4848
const webhooksRoutes = require('./lib/api/webhooks');
4949
const settingsRoutes = require('./lib/api/settings');
50+
const healthRoutes = require('./lib/api/health');
5051
const { SettingsHandler } = require('./lib/settings-handler');
5152

5253
let userHandler;
@@ -561,6 +562,7 @@ module.exports = done => {
561562
certsRoutes(db, server);
562563
webhooksRoutes(db, server);
563564
settingsRoutes(db, server, settingsHandler);
565+
healthRoutes(db, server);
564566

565567
if (process.env.NODE_ENV === 'test') {
566568
server.get(

lib/api/health.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
const Joi = require('joi');
4+
const tools = require('../tools');
5+
const { successRes } = require('../schemas/response/general-schemas');
6+
7+
module.exports = (db, server) => {
8+
server.get(
9+
{
10+
path: '/health',
11+
summary: 'Check the health of the API',
12+
description: 'Check the status of the WildDuck API service, that is if db is connected and readable/writable, same for redis.',
13+
tags: ['Health'],
14+
validationObjs: {
15+
requestBody: {},
16+
queryParams: {},
17+
pathParams: {},
18+
response: {
19+
200: {
20+
description: 'Success',
21+
model: Joi.object({ success: successRes })
22+
},
23+
500: {
24+
description: 'Failed',
25+
model: Joi.object({ success: successRes, message: Joi.string().required().description('Error message specifying what went wrong') })
26+
}
27+
}
28+
}
29+
},
30+
tools.responseWrapper(async (req, res) => {
31+
res.charSet('utf-8');
32+
33+
const currentTimestamp = Date.now() / 1000;
34+
35+
// 1) test that mongoDb is up
36+
try {
37+
const isConnected = await db.database.s.client.topology.isConnected();
38+
39+
if (!isConnected) {
40+
res.status(500);
41+
return res.json({
42+
success: false,
43+
message: 'DB is down'
44+
});
45+
}
46+
} catch (err) {
47+
res.status(500);
48+
return res.json({
49+
success: false,
50+
message: 'DB is down'
51+
});
52+
}
53+
54+
// 2) test that mongoDb is writeable
55+
56+
try {
57+
await db.database.collection(`${currentTimestamp}`).insert({ a: 'testWrite' });
58+
await db.database.collection(`${currentTimestamp}`).deleteOne({ a: 'testWrite' });
59+
} catch (err) {
60+
res.status(500);
61+
return res.json({
62+
success: false,
63+
message: 'Could not write to DB'
64+
});
65+
}
66+
67+
// 3) test redis PING
68+
db.redis.ping(err => {
69+
if (err) {
70+
res.status(500);
71+
return res.json({
72+
success: false,
73+
message: 'Redis is down'
74+
});
75+
}
76+
});
77+
78+
// 4) test if redis is writeable
79+
try {
80+
await db.redis.set(`${currentTimestamp}`, 'testVal');
81+
await db.redis.get(`${currentTimestamp}`);
82+
await db.redis.del(`${currentTimestamp}`);
83+
} catch (err) {
84+
res.status(500);
85+
return res.json({
86+
success: false,
87+
message: 'Redis is not writeable/readable'
88+
});
89+
}
90+
91+
res.status(200);
92+
return res.json({ success: true });
93+
})
94+
);
95+
};

0 commit comments

Comments
 (0)