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'; // Helper function to get API key from cookies function getApiKey(): string | null { const cookieStore = cookies(); return cookieStore.get(API_KEY_COOKIE)?.value || null; } // Set export const dynamic to force dynamic rendering export const dynamic = 'force-dynamic'; // Handler for GET requests (get DNSSEC status) export async function GET( request: NextRequest, { params }: { params: { domain: string } } ) { const logger = getLogger(); const startTime = Date.now(); const domain = params.domain; // Check if user is authenticated const apiKey = getApiKey(); if (!apiKey) { logger.log({ timestamp: new Date().toISOString(), method: 'GET', endpoint: `/api/vultr/domains/${domain}/dnssec`, statusCode: 401, responseTime: Date.now() - startTime, error: 'Authentication required' }); return NextResponse.json( { error: 'Authentication required' }, { status: 401 } ); } try { logger.log({ timestamp: new Date().toISOString(), method: 'GET', endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/dnssec`, apiKey }); const response = await fetch(`${VULTR_API_BASE_URL}/domains/${domain}/dnssec`, { headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json', }, }); const data = await response.json(); logger.log({ timestamp: new Date().toISOString(), method: 'GET', endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/dnssec`, statusCode: response.status, responseTime: Date.now() - startTime, apiKey }); if (!response.ok) { return NextResponse.json( { error: data.error || `Failed to fetch DNSSEC status for ${domain}` }, { status: response.status } ); } return NextResponse.json({ dnssec: data.dnssec_enabled ? "enabled" : "disabled" }); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; logger.log({ timestamp: new Date().toISOString(), method: 'GET', endpoint: `/api/vultr/domains/${domain}/dnssec`, statusCode: 500, responseTime: Date.now() - startTime, error: errorMessage, apiKey }); console.error(`Error fetching DNSSEC status for ${domain}:`, error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); } } // Handler for PUT requests (update DNSSEC status) export async function PUT( request: NextRequest, { params }: { params: { domain: string } } ) { const logger = getLogger(); const startTime = Date.now(); const domain = params.domain; // Check if user is authenticated const apiKey = getApiKey(); if (!apiKey) { logger.log({ timestamp: new Date().toISOString(), method: 'PUT', endpoint: `/api/vultr/domains/${domain}/dnssec`, statusCode: 401, responseTime: Date.now() - startTime, error: 'Authentication required' }); return NextResponse.json( { error: 'Authentication required' }, { status: 401 } ); } try { // Parse request body const body = await request.json(); // Validate required fields if (typeof body.enabled !== 'boolean') { logger.log({ timestamp: new Date().toISOString(), method: 'PUT', endpoint: `/api/vultr/domains/${domain}/dnssec`, statusCode: 400, responseTime: Date.now() - startTime, error: 'Missing or invalid enabled field', apiKey }); return NextResponse.json( { error: 'The enabled field is required and must be a boolean' }, { status: 400 } ); } logger.log({ timestamp: new Date().toISOString(), method: 'PUT', endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/dnssec`, apiKey, requestBody: { enabled: body.enabled } }); // Make request to Vultr API const response = await fetch(`${VULTR_API_BASE_URL}/domains/${domain}/dnssec`, { method: 'PUT', headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ enabled: body.enabled, }), }); logger.log({ timestamp: new Date().toISOString(), method: 'PUT', endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/dnssec`, statusCode: response.status, responseTime: Date.now() - startTime, apiKey }); if (!response.ok) { const data = await response.json(); return NextResponse.json( { error: data.error || `Failed to update DNSSEC status for ${domain}` }, { status: response.status } ); } // Return the updated DNSSEC status return NextResponse.json({ dnssec: body.enabled ? "enabled" : "disabled" }); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; logger.log({ timestamp: new Date().toISOString(), method: 'PUT', endpoint: `/api/vultr/domains/${domain}/dnssec`, statusCode: 500, responseTime: Date.now() - startTime, error: errorMessage, apiKey }); console.error(`Error updating DNSSEC status for ${domain}:`, error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); } }