|
| 1 | +import { Request, Response } from 'express'; |
| 2 | +import { prisma } from '../../utils/prisma.utils'; |
| 3 | +import { envConfig } from '../../config'; |
| 4 | + |
| 5 | +interface HealthStatus { |
| 6 | + success: boolean; |
| 7 | + message: string; |
| 8 | + timestamp: string; |
| 9 | + version: string; |
| 10 | + environment: string; |
| 11 | + uptime: number; |
| 12 | + memory: { |
| 13 | + used: number; |
| 14 | + total: number; |
| 15 | + }; |
| 16 | + system: { |
| 17 | + platform: string; |
| 18 | + nodeVersion: string; |
| 19 | + }; |
| 20 | + database?: { |
| 21 | + status: 'connected' | 'disconnected'; |
| 22 | + responseTime?: number; |
| 23 | + }; |
| 24 | + services?: { |
| 25 | + name: string; |
| 26 | + status: 'healthy' | 'unhealthy'; |
| 27 | + }[]; |
| 28 | +} |
| 29 | + |
| 30 | +export const healthCheck = async (_: Request, res: Response): Promise<void> => { |
| 31 | + const startTime = Date.now(); |
| 32 | + |
| 33 | + try { |
| 34 | + // Check database connectivity |
| 35 | + let dbStatus: HealthStatus['database'] = { |
| 36 | + status: 'disconnected', |
| 37 | + }; |
| 38 | + |
| 39 | + try { |
| 40 | + await prisma.$queryRaw`SELECT 1`; |
| 41 | + const dbResponseTime = Date.now() - startTime; |
| 42 | + dbStatus = { |
| 43 | + status: 'connected', |
| 44 | + responseTime: dbResponseTime, |
| 45 | + }; |
| 46 | + } catch (dbError) { |
| 47 | + console.error('Database health check failed:', dbError); |
| 48 | + dbStatus = { |
| 49 | + status: 'disconnected', |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + const healthData: HealthStatus = { |
| 54 | + success: true, |
| 55 | + message: 'Access Layer server is running', |
| 56 | + timestamp: new Date().toISOString(), |
| 57 | + version: '1.0.0', |
| 58 | + environment: envConfig.MODE || 'development', |
| 59 | + uptime: process.uptime(), |
| 60 | + memory: { |
| 61 | + used: |
| 62 | + Math.round((process.memoryUsage().heapUsed / 1024 / 1024) * 100) / |
| 63 | + 100, |
| 64 | + total: |
| 65 | + Math.round((process.memoryUsage().heapTotal / 1024 / 1024) * 100) / |
| 66 | + 100, |
| 67 | + }, |
| 68 | + system: { |
| 69 | + platform: process.platform, |
| 70 | + nodeVersion: process.version, |
| 71 | + }, |
| 72 | + database: dbStatus, |
| 73 | + services: [ |
| 74 | + { |
| 75 | + name: 'API Server', |
| 76 | + status: 'healthy', |
| 77 | + }, |
| 78 | + { |
| 79 | + name: 'Database', |
| 80 | + status: dbStatus.status === 'connected' ? 'healthy' : 'unhealthy', |
| 81 | + }, |
| 82 | + ], |
| 83 | + }; |
| 84 | + |
| 85 | + // Return 503 if database is disconnected in production |
| 86 | + const overallHealthy = |
| 87 | + dbStatus.status === 'connected' || |
| 88 | + envConfig.MODE !== 'production'; |
| 89 | + |
| 90 | + res.status(overallHealthy ? 200 : 503).json(healthData); |
| 91 | + } catch (error) { |
| 92 | + console.error('Health check failed:', error); |
| 93 | + res.status(500).json({ |
| 94 | + success: false, |
| 95 | + message: 'Health check failed', |
| 96 | + error: error instanceof Error ? error.message : 'Unknown error', |
| 97 | + }); |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +export const simpleHealthCheck = (_: Request, res: Response): void => { |
| 102 | + res.status(200).json({ |
| 103 | + success: true, |
| 104 | + message: 'OK', |
| 105 | + timestamp: new Date().toISOString(), |
| 106 | + }); |
| 107 | +}; |
0 commit comments