import { NextRequest, NextResponse } from 'next/server'; import { cookies } from 'next/headers'; import { getLogger } from '@/lib/logger'; // Base URL for Vultr API const VULTR_API_BASE_URL = 'https://api.vultr.com/v2'; // Cookie name for storing the API key const API_KEY_COOKIE = 'vultr_api_key'; // Set export const dynamic to force dynamic rendering export const dynamic = 'force-dynamic'; // Handler for GET requests (get logs) export async function GET(request: NextRequest) { const logger = getLogger(); return NextResponse.json({ logs: logger.getLogs() }); } // Handler for POST requests (set API key) export async function POST(request: NextRequest) { const logger = getLogger(); const startTime = Date.now(); try { const body = await request.json(); const { apiKey } = body; if (!apiKey) { logger.log({ timestamp: new Date().toISOString(), method: 'POST', endpoint: '/api/vultr', statusCode: 400, responseTime: Date.now() - startTime, error: 'API key is required' }); return NextResponse.json( { error: 'API key is required' }, { status: 400 } ); } // Validate API key by making a test request to Vultr API logger.log({ timestamp: new Date().toISOString(), method: 'GET', endpoint: `${VULTR_API_BASE_URL}/account`, apiKey }); const response = await fetch(`${VULTR_API_BASE_URL}/account`, { headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json', }, }); logger.log({ timestamp: new Date().toISOString(), method: 'GET', endpoint: `${VULTR_API_BASE_URL}/account`, statusCode: response.status, responseTime: Date.now() - startTime, apiKey }); if (!response.ok) { return NextResponse.json( { error: 'Invalid API key' }, { status: 401 } ); } // Set API key in cookie (HTTP only for security) const cookieStore = cookies(); cookieStore.set(API_KEY_COOKIE, apiKey, { httpOnly: true, secure: process.env.NODE_ENV === 'production', sameSite: 'strict', path: '/', // Expire in 7 days maxAge: 60 * 60 * 24 * 7, }); return NextResponse.json({ success: true }); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; logger.log({ timestamp: new Date().toISOString(), method: 'POST', endpoint: '/api/vultr', statusCode: 500, responseTime: Date.now() - startTime, error: errorMessage }); console.error('Error setting API key:', error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); } } // Handler for DELETE requests (clear API key) export async function DELETE(request: NextRequest) { const logger = getLogger(); const startTime = Date.now(); try { // Clear API key cookie const cookieStore = cookies(); cookieStore.delete(API_KEY_COOKIE); logger.log({ timestamp: new Date().toISOString(), method: 'DELETE', endpoint: '/api/vultr', statusCode: 200, responseTime: Date.now() - startTime }); return NextResponse.json({ success: true }); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; logger.log({ timestamp: new Date().toISOString(), method: 'DELETE', endpoint: '/api/vultr', statusCode: 500, responseTime: Date.now() - startTime, error: errorMessage }); console.error('Error clearing API key:', error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); } }