feat: enhance health check endpoint with database status check
This commit is contained in:
+20
-4
@@ -1,6 +1,7 @@
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import dotenv from 'dotenv';
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
@@ -11,10 +12,25 @@ app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
app.get('/api/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
message: 'Gameno API is running',
|
||||
timestamp: new Date().toISOString()
|
||||
const dbStates = {
|
||||
0: 'disconnected',
|
||||
1: 'connected',
|
||||
2: 'connecting',
|
||||
3: 'disconnecting',
|
||||
99: 'uninitialized'
|
||||
};
|
||||
|
||||
const dbStateCode = mongoose.connection.readyState;
|
||||
const dbStatus = dbStates[dbStateCode] || 'unknown';
|
||||
const isHealthy = dbStateCode === 1;
|
||||
|
||||
res.status(isHealthy ? 200 : 503).json({
|
||||
status: isHealthy ? 'healthy' : 'unhealthy',
|
||||
timestamp: new Date().toISOString(),
|
||||
uptime: process.uptime(),
|
||||
services: {
|
||||
database: dbStatus
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user