@nanocollective/nanocoder
Version:
A local-first CLI coding agent that brings the power of agentic coding tools like Claude Code and Gemini CLI to local models or controlled APIs like OpenRouter
55 lines • 1.89 kB
JavaScript
/**
* Health check middleware for HTTP servers
*/
/**
* Health check middleware for HTTP servers
*/
export function healthCheckMiddleware(healthChecks) {
return async (req, res, next) => {
if (req.path === '/health') {
try {
const health = await healthChecks.full();
const statusCode = health.status === 'healthy'
? 200
: health.status === 'degraded'
? 200
: 503;
res.status(statusCode).json({
status: health.status,
timestamp: health.timestamp,
score: health.score,
checks: health.checks.map(check => ({
name: check.name,
status: check.status,
score: check.score,
})),
});
}
catch (error) {
res.status(503).json({
status: 'unhealthy',
timestamp: new Date().toISOString(),
error: error instanceof Error ? error.message : 'Unknown error',
});
}
}
else if (req.path === '/health/ready') {
const ready = await healthChecks.ready();
const statusCode = ready ? 200 : 503;
res.status(statusCode).json({ ready });
}
else if (req.path === '/health/live') {
const alive = healthChecks.alive();
const statusCode = alive ? 200 : 503;
res.status(statusCode).json({ alive });
}
else if (req.path === '/metrics') {
const metrics = healthChecks.metrics();
res.json(metrics);
}
else {
next();
}
};
}
//# sourceMappingURL=http-middleware.js.map