Files
dockship/src/app/api/deploy/route.ts
T
2026-02-15 17:40:24 +01:00

91 lines
2.0 KiB
TypeScript

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<NextResponse> {
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<NextResponse> {
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<NextResponse> {
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",
});
}