import { NextRequest, NextResponse } from "next/server"; import { startService, stopService, restartService, getServiceLogs, type ServiceRole, } from "@/lib/docker"; export const dynamic = "force-dynamic"; export const runtime = "nodejs"; export const maxDuration = 120; /** * POST /api/comfy * Body: { action: "start" | "stop" | "restart" } * * Start / stop / restart ComfyUI. Optional `extraArgs` for start (passed * through COMFY_EXTRA_ARGS env var). */ export async function POST(req: NextRequest) { const body = (await req.json()) as { action: "start" | "stop" | "restart"; extraArgs?: string; }; const role: ServiceRole = "comfyui"; if (body.action === "start") { if (body.extraArgs) { process.env.COMFY_EXTRA_ARGS = body.extraArgs; } const result = await startService(role); return NextResponse.json(result, { status: result.ok ? 200 : 500 }); } if (body.action === "stop") { const result = await stopService(role); return NextResponse.json(result, { status: result.ok ? 200 : 500 }); } if (body.action === "restart") { const result = await restartService(role); return NextResponse.json(result, { status: result.ok ? 200 : 500 }); } return NextResponse.json({ error: "unknown action" }, { status: 400 }); } /** * GET /api/comfy?logs=1&tail=500 * Returns comfyui logs. */ export async function GET(req: NextRequest) { const withLogs = req.nextUrl.searchParams.get("logs"); if (withLogs) { const tail = Number(req.nextUrl.searchParams.get("tail") || 500); const logs = await getServiceLogs("comfyui", tail); return NextResponse.json({ ok: true, logs }); } return NextResponse.json({ ok: true }); }