nexus-dashboard/app/api/vms/[id]/route.ts
2026-02-01 18:42:22 +00:00

50 lines
1.2 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getVMConfig, getVMStatus, getVMStats, getVMList } from '@/lib/ssh';
export async function GET(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
// Get VM basic info from list
const vms = await getVMList();
const vm = vms.find(v => v.vmid === id);
if (!vm) {
return NextResponse.json(
{ error: 'VM not found' },
{ status: 404 }
);
}
// Get detailed config
const config = await getVMConfig(id);
const status = await getVMStatus(id);
// Get stats only if VM is running
let stats = { cpu: 0, mem: { used: 0, total: 0, percent: 0 }, disk: { percent: 0 } };
if (status === 'running') {
try {
stats = await getVMStats(id, vm.name);
} catch (error) {
console.error('Error fetching stats:', error);
}
}
return NextResponse.json({
...vm,
config,
status,
stats
});
} catch (error) {
console.error('Error fetching VM details:', error);
return NextResponse.json(
{ error: 'Failed to fetch VM details' },
{ status: 500 }
);
}
}