import { NextResponse } from 'next/server' import { getPrismaClient } from '@/lib/database/trades' /** * Health check endpoint for DNS failover monitoring * Returns 200 OK only if server AND database are healthy * * CRITICAL: If database is down, this returns 503 * This triggers DNS failover to backup server */ export async function GET() { const startTime = Date.now() try { // Check database connectivity with 5-second timeout const prisma = getPrismaClient() const dbCheck = await Promise.race([ prisma.$queryRaw`SELECT 1 as health_check`, new Promise((_, reject) => setTimeout(() => reject(new Error('Database timeout')), 5000) ) ]) const responseTime = Date.now() - startTime return NextResponse.json( { status: 'healthy', timestamp: new Date().toISOString(), uptime: process.uptime(), database: 'connected', responseTime: `${responseTime}ms` }, { status: 200 } ) } catch (error: any) { // Database failure - return 503 to trigger failover console.error('❌ Health check failed - Database unavailable:', error.message) return NextResponse.json( { status: 'unhealthy', timestamp: new Date().toISOString(), uptime: process.uptime(), database: 'disconnected', error: error.message, responseTime: `${Date.now() - startTime}ms` }, { status: 503 } // Service Unavailable - triggers DNS failover ) } }