64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { connectDB } from '@/lib/mongodb'
|
|
import { createClient } from 'redis'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const startTime = Date.now()
|
|
|
|
const healthCheck = {
|
|
status: 'healthy',
|
|
timestamp: new Date().toISOString(),
|
|
uptime: process.uptime(),
|
|
environment: process.env.NODE_ENV,
|
|
version: process.env.npm_package_version || '1.0.0',
|
|
checks: {
|
|
database: { status: 'unknown' as 'healthy' | 'unhealthy' | 'unknown' },
|
|
redis: { status: 'unknown' as 'healthy' | 'unhealthy' | 'unknown' },
|
|
},
|
|
responseTime: 0,
|
|
}
|
|
|
|
// Check database connection
|
|
try {
|
|
await connectDB()
|
|
healthCheck.checks.database.status = 'healthy'
|
|
} catch (error) {
|
|
healthCheck.checks.database.status = 'unhealthy'
|
|
healthCheck.status = 'unhealthy'
|
|
}
|
|
|
|
// Check Redis connection
|
|
try {
|
|
const redis = createClient({
|
|
url: process.env.REDIS_URL || 'redis://localhost:6379',
|
|
})
|
|
|
|
await redis.connect()
|
|
await redis.ping()
|
|
await redis.disconnect()
|
|
|
|
healthCheck.checks.redis.status = 'healthy'
|
|
} catch (error) {
|
|
healthCheck.checks.redis.status = 'unhealthy'
|
|
healthCheck.status = 'unhealthy'
|
|
}
|
|
|
|
// Calculate response time
|
|
healthCheck.responseTime = Date.now() - startTime
|
|
|
|
// Return appropriate status code
|
|
const statusCode = healthCheck.status === 'healthy' ? 200 : 503
|
|
|
|
return NextResponse.json(healthCheck, { status: statusCode })
|
|
}
|
|
|
|
// Readiness check - simpler check for container orchestration
|
|
export async function HEAD(request: NextRequest) {
|
|
try {
|
|
// Quick check if the application is ready to serve requests
|
|
return new NextResponse(null, { status: 200 })
|
|
} catch (error) {
|
|
return new NextResponse(null, { status: 503 })
|
|
}
|
|
}
|