@@ -81,6 +81,45 @@ export const cleanup = async () => {
8181
8282app . get ( '/hello' , ( req , res ) => { return res . status ( 200 ) . send ( "Hello, World!" ) } )
8383
84+ app . get ( '/health' , async ( req , res ) => {
85+ try {
86+ const mongoose = await import ( 'mongoose' ) ;
87+ const dbStatus = mongoose . connection . readyState === 1 ? 'healthy' : 'unhealthy' ;
88+
89+ let redisStatus = 'not_configured' ;
90+ if ( process . env . REDIS_URL ) {
91+ try {
92+ const redis = await import ( 'redis' ) ;
93+ const client = redis . createClient ( { url : process . env . REDIS_URL } ) ;
94+ await client . connect ( ) ;
95+ await client . ping ( ) ;
96+ await client . disconnect ( ) ;
97+ redisStatus = 'healthy' ;
98+ } catch ( error ) {
99+ redisStatus = 'unhealthy' ;
100+ }
101+ }
102+
103+ const health = {
104+ status : dbStatus === 'healthy' ? 'healthy' : 'unhealthy' ,
105+ timestamp : new Date ( ) . toISOString ( ) ,
106+ services : {
107+ database : dbStatus ,
108+ redis : redisStatus
109+ } ,
110+ uptime : process . uptime ( )
111+ } ;
112+
113+ res . status ( health . status === 'healthy' ? 200 : 503 ) . json ( health ) ;
114+ } catch ( error ) {
115+ res . status ( 503 ) . json ( {
116+ status : 'unhealthy' ,
117+ timestamp : new Date ( ) . toISOString ( ) ,
118+ error : error . message
119+ } ) ;
120+ }
121+ } ) ;
122+
84123// 404 handler for unmatched routes
85124app . use ( '*' , ( req , res , next ) => {
86125 const error = new Error ( `Route ${ req . originalUrl } not found` ) ;
0 commit comments