import { NextRequest, NextResponse } from 'next/server' import { getDriftService } from '@/lib/drift/client' /** * Manual Drift reconnection endpoint * Forces reconnection to clear WebSocket subscriptions and prevent memory leaks * * POST /api/drift/reconnect * Authorization: Bearer */ export async function POST(request: NextRequest) { try { // Verify API key const authHeader = request.headers.get('authorization') const apiKey = process.env.API_SECRET_KEY if (!authHeader || !authHeader.startsWith('Bearer ') || authHeader.slice(7) !== apiKey) { return NextResponse.json( { success: false, error: 'Unauthorized' }, { status: 401 } ) } console.log('🔄 Manual reconnection requested via API...') const driftService = getDriftService() // Force reconnection by calling private method through type assertion await (driftService as any).reconnect() console.log('✅ Manual reconnection complete') return NextResponse.json({ success: true, message: 'Drift connection refreshed successfully', timestamp: new Date().toISOString() }) } catch (error: any) { console.error('❌ Manual reconnection failed:', error) return NextResponse.json( { success: false, error: error.message || 'Failed to reconnect to Drift Protocol', timestamp: new Date().toISOString() }, { status: 500 } ) } } /** * Get reconnection status and next scheduled reconnection time * * GET /api/drift/reconnect */ export async function GET(request: NextRequest) { try { const driftService = getDriftService() return NextResponse.json({ success: true, status: { initialized: (driftService as any).isInitialized, hasReconnectTimer: !!(driftService as any).reconnectTimer, reconnectIntervalHours: (driftService as any).reconnectIntervalMs / 1000 / 60 / 60, message: 'Automatic reconnection runs every 4 hours to prevent memory leaks' } }) } catch (error: any) { return NextResponse.json( { success: false, error: error.message }, { status: 500 } ) } }