localgreenchain/pages/api/health/index.ts
Claude 5ea8bab5c3
Add production deployment infrastructure (Agent 4)
- Docker: Multi-stage Dockerfile with security hardening, docker-compose
  for production and development environments
- Environment: Comprehensive .env.example with all config options,
  lib/config/env.ts for typed environment validation
- Logging: Structured JSON logging with request/response middleware
- Monitoring: Prometheus metrics endpoint, Grafana dashboard, health
  checks (liveness/readiness probes)
- Security: Security headers, rate limiting, CORS middleware
- CI/CD: GitHub Actions workflows for CI, production deploy, and
  preview deployments
- Error tracking: Sentry integration foundation

Files created:
- Docker: Dockerfile, docker-compose.yml, docker-compose.dev.yml, .dockerignore
- Config: lib/config/env.ts, lib/config/index.ts
- Logging: lib/logging/logger.ts, lib/logging/middleware.ts
- Monitoring: lib/monitoring/sentry.ts, lib/monitoring/metrics.ts,
  lib/monitoring/health.ts
- Security: lib/security/headers.ts, lib/security/rateLimit.ts,
  lib/security/cors.ts
- API: pages/api/health/*, pages/api/metrics.ts
- Infra: infra/prometheus/prometheus.yml, infra/grafana/*
2025-11-23 03:54:03 +00:00

52 lines
1.4 KiB
TypeScript

/**
* Health Check Endpoint
* Agent 4: Production Deployment
*
* GET /api/health
* Returns overall application health status with component checks.
*/
import type { NextApiRequest, NextApiResponse } from 'next';
import { healthChecks, HealthStatus } from '../../../lib/monitoring';
import { withLogging } from '../../../lib/logging';
interface HealthResponse extends HealthStatus {}
interface ErrorResponse {
error: string;
message: string;
}
async function handler(
req: NextApiRequest,
res: NextApiResponse<HealthResponse | ErrorResponse>
) {
if (req.method !== 'GET') {
res.setHeader('Allow', ['GET']);
return res.status(405).json({
error: 'Method Not Allowed',
message: `Method ${req.method} is not allowed`,
});
}
try {
const health = await healthChecks.runAll();
// Set appropriate status code based on health
const statusCode = health.status === 'healthy' ? 200 : health.status === 'degraded' ? 200 : 503;
// Add cache headers - don't cache health checks
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
return res.status(statusCode).json(health);
} catch (error) {
return res.status(503).json({
error: 'Health Check Failed',
message: error instanceof Error ? error.message : 'Unknown error',
});
}
}
export default withLogging(handler);