feat: enhance health check endpoint with database status check
This commit is contained in:
@@ -5,6 +5,7 @@ const express = require('express');
|
||||
const cors = require('cors');
|
||||
const helmet = require('helmet');
|
||||
const rateLimit = require('express-rate-limit');
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const config = require('./config/config');
|
||||
const connectDB = require('./config/db');
|
||||
@@ -53,10 +54,25 @@ app.use('/api', limiter);
|
||||
|
||||
// ── Routes ────────────────────────────────────────────────────────────────────
|
||||
app.get('/api/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
message: 'Gameno API is running',
|
||||
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
|
||||
},
|
||||
environment: config.NODE_ENV
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user