UNPKG

cube-ms

Version:

Production-ready microservice framework with health monitoring, validation, error handling, and Docker Swarm support

72 lines (66 loc) 1.78 kB
/** * Health check routes */ export function setupHealthRoutes(service) { // Basic health check service.addRoute('get', '/health', (req, res, logger) => { logger.debug('Health check requested'); res.json({ success: true, status: 'healthy', timestamp: new Date().toISOString(), uptime: process.uptime(), memory: process.memoryUsage(), pid: process.pid }); }); // Detailed health check with database status service.addRoute('get', '/health/detailed', async (req, res, logger) => { logger.debug('Detailed health check requested'); try { const dbStatus = await checkDatabaseHealth(service); res.json({ success: true, status: 'healthy', timestamp: new Date().toISOString(), uptime: process.uptime(), memory: process.memoryUsage(), database: dbStatus, pid: process.pid, version: process.version }); } catch (error) { logger.error('Health check failed', error); res.status(503).json({ success: false, status: 'unhealthy', error: error.message, timestamp: new Date().toISOString() }); } }); } /** * Check database connectivity */ async function checkDatabaseHealth(service) { try { const db = service.getDatabase(); if (!db) { return { status: 'disconnected', message: 'Database not configured' }; } // Simple ping to check connection await db.admin().ping(); return { status: 'connected', name: db.databaseName, timestamp: new Date().toISOString() }; } catch (error) { return { status: 'error', message: error.message, timestamp: new Date().toISOString() }; } }