import { NextRequest, NextResponse } from "next/server"; import testAuth from "../../../lib/server/auth"; import { StatusCodes } from "http-status-codes"; import { deployApp, removeDeployment, toggleDeployment, } from "@/core/deployment"; import { Application, Port } from "@/lib/types"; export async function POST(req: NextRequest): Promise { const auth = await testAuth(req); const { name, app, ports, }: { name: string; app: Application; ports: Port[] } = await req.json(); if (auth) { return NextResponse.json( { message: auth }, { status: StatusCodes.UNAUTHORIZED }, ); } if (!name || !app) { return NextResponse.json( { message: "Body missing name, appId, nodeId" }, { status: StatusCodes.BAD_REQUEST }, ); } const deployId = await deployApp(name, app, ports); return NextResponse.json({ message: "ok", deployId, }); } export async function PUT(req: NextRequest): Promise { const auth = await testAuth(req); const { deployId, action }: { deployId: string; action: "start" | "stop" } = await req.json(); if (auth) { return NextResponse.json( { message: auth }, { status: StatusCodes.UNAUTHORIZED }, ); } if (!deployId || !action) { return NextResponse.json( { message: "Body missing deployId or action" }, { status: StatusCodes.BAD_REQUEST }, ); } await toggleDeployment(deployId, action); return NextResponse.json({ message: "ok", }); } export async function DELETE(req: NextRequest): Promise { const deployId = req.nextUrl.searchParams.get("deployId"); const auth = await testAuth(req); if (auth) { return NextResponse.json( { message: auth }, { status: StatusCodes.UNAUTHORIZED }, ); } if (!deployId) { return NextResponse.json( { message: "Body missing deployId" }, { status: StatusCodes.BAD_REQUEST }, ); } await removeDeployment(deployId); return NextResponse.json({ message: "ok", }); }