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 SOA information) 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}/soa`, 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}/soa`, apiKey }); const response = await fetch(`${VULTR_API_BASE_URL}/domains/${domain}/soa`, { 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}/soa`, statusCode: response.status, responseTime: Date.now() - startTime, apiKey }); if (!response.ok) { return NextResponse.json( { error: data.error || `Failed to fetch SOA record for ${domain}` }, { status: response.status } ); } return NextResponse.json({ soa: { ns1: data.soa?.ns_primary || '', email: data.soa?.email || '', } }); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; logger.log({ timestamp: new Date().toISOString(), method: 'GET', endpoint: `/api/vultr/domains/${domain}/soa`, statusCode: 500, responseTime: Date.now() - startTime, error: errorMessage, apiKey }); console.error(`Error fetching SOA record for ${domain}:`, error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); } } // Handler for PUT requests (update SOA information) 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}/soa`, 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 (!body.ns1 || !body.email) { logger.log({ timestamp: new Date().toISOString(), method: 'PUT', endpoint: `/api/vultr/domains/${domain}/soa`, statusCode: 400, responseTime: Date.now() - startTime, error: 'Missing required fields', apiKey }); return NextResponse.json( { error: 'Primary nameserver and email are required' }, { status: 400 } ); } logger.log({ timestamp: new Date().toISOString(), method: 'PUT', endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/soa`, apiKey, requestBody: { ns_primary: body.ns1, email: body.email } }); // Make request to Vultr API const response = await fetch(`${VULTR_API_BASE_URL}/domains/${domain}/soa`, { method: 'PATCH', headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ ns_primary: body.ns1, email: body.email, }), }); logger.log({ timestamp: new Date().toISOString(), method: 'PUT', endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/soa`, 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 SOA record for ${domain}` }, { status: response.status } ); } // Return the updated SOA information return NextResponse.json({ soa: { ns1: body.ns1, email: body.email, } }); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; logger.log({ timestamp: new Date().toISOString(), method: 'PUT', endpoint: `/api/vultr/domains/${domain}/soa`, statusCode: 500, responseTime: Date.now() - startTime, error: errorMessage, apiKey }); console.error(`Error updating SOA record for ${domain}:`, error); return NextResponse.json( { error: 'Internal server error' },{ status: 500 } ); } }