initial commit
This commit is contained in:
commit
8a67761c9a
83 changed files with 15510 additions and 0 deletions
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