feat: enhance health check endpoint with database status check

This commit is contained in:
2026-08-12 05:26:00 +03:30
parent f04c797be6
commit 07edb98a05
2 changed files with 39 additions and 7 deletions
+20 -4
View File
@@ -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
}
});
});