45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
import { Router } from 'express';
|
|
import { checkDatabaseHealth, isDatabaseConfigured } from '../services/db.js';
|
|
|
|
export const healthRouter = Router();
|
|
|
|
function healthHandler(req, res) {
|
|
res.status(200).json({
|
|
ok: true,
|
|
service: 'krow-core-api',
|
|
version: process.env.SERVICE_VERSION || 'dev',
|
|
requestId: req.requestId,
|
|
});
|
|
}
|
|
|
|
healthRouter.get('/health', healthHandler);
|
|
healthRouter.get('/healthz', healthHandler);
|
|
|
|
healthRouter.get('/readyz', async (req, res) => {
|
|
if (!isDatabaseConfigured()) {
|
|
return res.status(503).json({
|
|
ok: false,
|
|
service: 'krow-core-api',
|
|
status: 'DATABASE_NOT_CONFIGURED',
|
|
requestId: req.requestId,
|
|
});
|
|
}
|
|
|
|
const healthy = await checkDatabaseHealth().catch(() => false);
|
|
if (!healthy) {
|
|
return res.status(503).json({
|
|
ok: false,
|
|
service: 'krow-core-api',
|
|
status: 'DATABASE_UNAVAILABLE',
|
|
requestId: req.requestId,
|
|
});
|
|
}
|
|
|
|
return res.status(200).json({
|
|
ok: true,
|
|
service: 'krow-core-api',
|
|
status: 'READY',
|
|
requestId: req.requestId,
|
|
});
|
|
});
|