Repository navigation
Add /api/health endpoint with MongoDB/Redis dependency checks
#86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| 'use strict'; | ||
|
|
||
| const mockRedis = { | ||
| status: 'ready', | ||
| ping: jest.fn(), | ||
| }; | ||
|
|
||
| jest.mock('mongoose', () => ({ | ||
| connection: { | ||
| readyState: 1, | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('@urbackend/common', () => ({ | ||
| redis: mockRedis, | ||
| })); | ||
|
|
||
| const mongoose = require('mongoose'); | ||
| const express = require('express'); | ||
| const request = require('supertest'); | ||
| const healthRoute = require('../routes/health'); | ||
|
|
||
| describe('health route', () => { | ||
| let app; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mongoose.connection.readyState = 1; | ||
| mockRedis.status = 'ready'; | ||
| mockRedis.ping.mockResolvedValue('PONG'); | ||
|
|
||
| app = express(); | ||
| app.use('/api/health', healthRoute); | ||
| }); | ||
|
|
||
| test('returns ok when mongodb and redis are connected', async () => { | ||
| const res = await request(app).get('/api/health'); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(res.body.status).toBe('ok'); | ||
| expect(res.body.dependencies).toEqual({ | ||
| mongodb: 'connected', | ||
| redis: 'connected', | ||
| }); | ||
| expect(typeof res.body.timestamp).toBe('string'); | ||
| expect(mockRedis.ping).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('returns error when mongodb is disconnected', async () => { | ||
| mongoose.connection.readyState = 0; | ||
|
|
||
| const res = await request(app).get('/api/health'); | ||
|
|
||
| expect(res.status).toBe(503); | ||
| expect(res.body.status).toBe('error'); | ||
| expect(res.body.dependencies).toEqual({ | ||
| mongodb: 'disconnected', | ||
| redis: 'connected', | ||
| }); | ||
| }); | ||
|
|
||
| test('returns error when redis is not responsive', async () => { | ||
| mockRedis.ping.mockRejectedValue(new Error('redis unavailable')); | ||
|
|
||
| const res = await request(app).get('/api/health'); | ||
|
|
||
| expect(res.status).toBe(503); | ||
| expect(res.body.status).toBe('error'); | ||
| expect(res.body.dependencies).toEqual({ | ||
| mongodb: 'connected', | ||
| redis: 'disconnected', | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| const mongoose = require('mongoose'); | ||
| const { redis } = require('@urbackend/common'); | ||
| const REDIS_PING_TIMEOUT_MS = 500; | ||
|
|
||
| const getHealth = async (req, res) => { | ||
| const isMongoConnected = mongoose.connection.readyState === 1; | ||
|
|
||
| let isRedisConnected = false; | ||
| if (redis?.status === 'ready' && typeof redis.ping === 'function') { | ||
| try { | ||
| const pingResponse = await Promise.race([ | ||
| redis.ping(), | ||
| new Promise((resolve) => setTimeout(() => resolve('TIMEOUT'), REDIS_PING_TIMEOUT_MS)), | ||
| ]); | ||
| isRedisConnected = pingResponse === 'PONG'; | ||
|
Comment on lines
+11
to
+15
|
||
| } catch (_error) { | ||
| isRedisConnected = false; | ||
| } | ||
| } | ||
|
|
||
| const status = isMongoConnected && isRedisConnected ? 'ok' : 'error'; | ||
|
|
||
| return res.status(status === 'ok' ? 200 : 503).json({ | ||
| status, | ||
| timestamp: new Date().toISOString(), | ||
| dependencies: { | ||
| mongodb: isMongoConnected ? 'connected' : 'disconnected', | ||
| redis: isRedisConnected ? 'connected' : 'disconnected', | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| module.exports = { | ||
| getHealth, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| const express = require('express'); | ||
| const { getHealth } = require('../controllers/health.controller'); | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| router.get('/', getHealth); | ||
|
|
||
| module.exports = router; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The controller introduces a Redis ping timeout guard, but the tests only cover the case where
ping()rejects. This doesn't verify the intended behavior whenping()hangs (the case the timeout is meant to protect against). Add a test wheremockRedis.pingreturns a never-resolving promise and assert the endpoint still responds (e.g., using Jest fake timers / advancing time) and marks Redis as disconnected.