initial commit
This commit is contained in:
commit
8a67761c9a
83 changed files with 15510 additions and 0 deletions
213
app/api/vultr/domains/[domain]/dnssec/route.ts
Normal file
213
app/api/vultr/domains/[domain]/dnssec/route.ts
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
187
app/api/vultr/domains/[domain]/records/[recordId]/route.ts
Normal file
187
app/api/vultr/domains/[domain]/records/[recordId]/route.ts
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
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 PATCH requests (update DNS record)
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { domain: string; recordId: string } }
|
||||
) {
|
||||
const logger = getLogger();
|
||||
const startTime = Date.now();
|
||||
const { domain, recordId } = params;
|
||||
|
||||
// Check if user is authenticated
|
||||
const apiKey = getApiKey();
|
||||
if (!apiKey) {
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'PATCH',
|
||||
endpoint: `/api/vultr/domains/${domain}/records/${recordId}`,
|
||||
statusCode: 401,
|
||||
responseTime: Date.now() - startTime,
|
||||
error: 'Authentication required'
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: 'Authentication required' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'PATCH',
|
||||
endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/records/${recordId}`,
|
||||
requestBody: body,
|
||||
apiKey
|
||||
});
|
||||
|
||||
const response = await fetch(`${VULTR_API_BASE_URL}/domains/${domain}/records/${recordId}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'PATCH',
|
||||
endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/records/${recordId}`,
|
||||
statusCode: response.status,
|
||||
responseTime: Date.now() - startTime,
|
||||
apiKey
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: data.error || `Failed to update DNS record ${recordId}` },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ record: data.record });
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'PATCH',
|
||||
endpoint: `/api/vultr/domains/${domain}/records/${recordId}`,
|
||||
statusCode: 500,
|
||||
responseTime: Date.now() - startTime,
|
||||
error: errorMessage,
|
||||
apiKey
|
||||
});
|
||||
|
||||
console.error(`Error updating DNS record ${recordId}:`, error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Handler for DELETE requests (delete DNS record)
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { domain: string; recordId: string } }
|
||||
) {
|
||||
const logger = getLogger();
|
||||
const startTime = Date.now();
|
||||
const { domain, recordId } = params;
|
||||
|
||||
// Check if user is authenticated
|
||||
const apiKey = getApiKey();
|
||||
if (!apiKey) {
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'DELETE',
|
||||
endpoint: `/api/vultr/domains/${domain}/records/${recordId}`,
|
||||
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: 'DELETE',
|
||||
endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/records/${recordId}`,
|
||||
apiKey
|
||||
});
|
||||
|
||||
const response = await fetch(`${VULTR_API_BASE_URL}/domains/${domain}/records/${recordId}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'DELETE',
|
||||
endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/records/${recordId}`,
|
||||
statusCode: response.status,
|
||||
responseTime: Date.now() - startTime,
|
||||
apiKey
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
return NextResponse.json(
|
||||
{ error: errorData.error || `Failed to delete DNS record ${recordId}` },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
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/domains/${domain}/records/${recordId}`,
|
||||
statusCode: 500,
|
||||
responseTime: Date.now() - startTime,
|
||||
error: errorMessage,
|
||||
apiKey
|
||||
});
|
||||
|
||||
console.error(`Error deleting DNS record ${recordId}:`, error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
187
app/api/vultr/domains/[domain]/records/route.ts
Normal file
187
app/api/vultr/domains/[domain]/records/route.ts
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
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 (list DNS records)
|
||||
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}/records`,
|
||||
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}/records`,
|
||||
apiKey
|
||||
});
|
||||
|
||||
const response = await fetch(`${VULTR_API_BASE_URL}/domains/${domain}/records`, {
|
||||
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}/records`,
|
||||
statusCode: response.status,
|
||||
responseTime: Date.now() - startTime,
|
||||
apiKey
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: data.error || `Failed to fetch DNS records for ${domain}` },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ records: data.records });
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'GET',
|
||||
endpoint: `/api/vultr/domains/${domain}/records`,
|
||||
statusCode: 500,
|
||||
responseTime: Date.now() - startTime,
|
||||
error: errorMessage,
|
||||
apiKey
|
||||
});
|
||||
|
||||
console.error(`Error fetching DNS records for ${domain}:`, error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Handler for POST requests (create DNS record)
|
||||
export async function POST(
|
||||
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: 'POST',
|
||||
endpoint: `/api/vultr/domains/${domain}/records`,
|
||||
statusCode: 401,
|
||||
responseTime: Date.now() - startTime,
|
||||
error: 'Authentication required'
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: 'Authentication required' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'POST',
|
||||
endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/records`,
|
||||
requestBody: body,
|
||||
apiKey
|
||||
});
|
||||
|
||||
const response = await fetch(`${VULTR_API_BASE_URL}/domains/${domain}/records`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'POST',
|
||||
endpoint: `${VULTR_API_BASE_URL}/domains/${domain}/records`,
|
||||
statusCode: response.status,
|
||||
responseTime: Date.now() - startTime,
|
||||
apiKey
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: data.error || `Failed to create DNS record for ${domain}` },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ record: data.record });
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'POST',
|
||||
endpoint: `/api/vultr/domains/${domain}/records`,
|
||||
statusCode: 500,
|
||||
responseTime: Date.now() - startTime,
|
||||
error: errorMessage,
|
||||
apiKey
|
||||
});
|
||||
|
||||
console.error(`Error creating DNS record for ${domain}:`, error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
219
app/api/vultr/domains/[domain]/soa/route.ts
Normal file
219
app/api/vultr/domains/[domain]/soa/route.ts
Normal file
|
|
@ -0,0 +1,219 @@
|
|||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
208
app/api/vultr/domains/route.ts
Normal file
208
app/api/vultr/domains/route.ts
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
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 (list domains)
|
||||
export async function GET(request: NextRequest) {
|
||||
const logger = getLogger();
|
||||
const startTime = Date.now();
|
||||
|
||||
// Check if user is authenticated
|
||||
const apiKey = getApiKey();
|
||||
if (!apiKey) {
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'GET',
|
||||
endpoint: '/api/vultr/domains',
|
||||
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`,
|
||||
apiKey
|
||||
});
|
||||
|
||||
const response = await fetch(`${VULTR_API_BASE_URL}/domains`, {
|
||||
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`,
|
||||
statusCode: response.status,
|
||||
responseTime: Date.now() - startTime,
|
||||
apiKey
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: data.error || 'Failed to fetch domains' },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ domains: data.domains });
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'GET',
|
||||
endpoint: '/api/vultr/domains',
|
||||
statusCode: 500,
|
||||
responseTime: Date.now() - startTime,
|
||||
error: errorMessage,
|
||||
apiKey
|
||||
});
|
||||
|
||||
console.error('Error fetching domains:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Handler for POST requests (create domain)
|
||||
export async function POST(request: NextRequest) {
|
||||
const logger = getLogger();
|
||||
const startTime = Date.now();
|
||||
|
||||
// Check if user is authenticated
|
||||
const apiKey = getApiKey();
|
||||
if (!apiKey) {
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'POST',
|
||||
endpoint: '/api/vultr/domains',
|
||||
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.domain || !body.serverip) {
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'POST',
|
||||
endpoint: '/api/vultr/domains',
|
||||
statusCode: 400,
|
||||
responseTime: Date.now() - startTime,
|
||||
error: 'Missing required fields',
|
||||
apiKey
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: 'Domain name and server IP are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'POST',
|
||||
endpoint: `${VULTR_API_BASE_URL}/domains`,
|
||||
apiKey,
|
||||
requestBody: { domain: body.domain, serverip: body.serverip }
|
||||
});
|
||||
|
||||
// Make request to Vultr API
|
||||
const response = await fetch(`${VULTR_API_BASE_URL}/domains`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
domain: body.domain,
|
||||
serverip: body.serverip,
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'POST',
|
||||
endpoint: `${VULTR_API_BASE_URL}/domains`,
|
||||
statusCode: response.status,
|
||||
responseTime: Date.now() - startTime,
|
||||
apiKey
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: data.error || 'Failed to create domain' },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
// Return the newly created domain
|
||||
return NextResponse.json({
|
||||
domain: {
|
||||
domain: body.domain,
|
||||
date_created: new Date().toISOString()
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
|
||||
logger.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
method: 'POST',
|
||||
endpoint: '/api/vultr/domains',
|
||||
statusCode: 500,
|
||||
responseTime: Date.now() - startTime,
|
||||
error: errorMessage,
|
||||
apiKey
|
||||
});
|
||||
|
||||
console.error('Error creating domain:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue