|
| 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