73 lines
1.7 KiB
JavaScript
73 lines
1.7 KiB
JavaScript
// node_api/routes/health.js
|
|
const express = require('express');
|
|
const axios = require('axios');
|
|
const { getPool } = require('../db/connection');
|
|
|
|
const router = express.Router();
|
|
|
|
// Get database pool
|
|
const pool = getPool();
|
|
|
|
/**
|
|
* Health check endpoint
|
|
*/
|
|
router.get('/', async (req, res) => {
|
|
const healthcheck = {
|
|
uptime: process.uptime(),
|
|
message: 'OK',
|
|
timestamp: new Date().toISOString(),
|
|
services: {}
|
|
};
|
|
|
|
try {
|
|
// Check database connection
|
|
try {
|
|
const dbResult = await pool.query('SELECT NOW()');
|
|
healthcheck.services.database = {
|
|
status: 'healthy',
|
|
timestamp: dbResult.rows[0].now
|
|
};
|
|
} catch (dbError) {
|
|
healthcheck.services.database = {
|
|
status: 'unhealthy',
|
|
error: dbError.message
|
|
};
|
|
}
|
|
|
|
// Check Directus connection
|
|
try {
|
|
const directusResponse = await axios.get(`${process.env.DIRECTUS_URL}/server/health`, {
|
|
timeout: 5000
|
|
});
|
|
healthcheck.services.directus = {
|
|
status: 'healthy',
|
|
version: directusResponse.data.status || 'unknown'
|
|
};
|
|
} catch (directusError) {
|
|
healthcheck.services.directus = {
|
|
status: 'unhealthy',
|
|
error: directusError.message
|
|
};
|
|
}
|
|
|
|
// Determine overall health
|
|
const allServicesHealthy = Object.values(healthcheck.services)
|
|
.every(service => service.status === 'healthy');
|
|
|
|
if (allServicesHealthy) {
|
|
res.status(200).json(healthcheck);
|
|
} else {
|
|
res.status(503).json(healthcheck);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Health check error:', error);
|
|
res.status(503).json({
|
|
...healthcheck,
|
|
message: 'Service Unavailable',
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
module.exports = router; |