From 07a37f92df9775273f79336c81883dd463fd333c Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 3 Jan 2026 20:15:58 +0100 Subject: [PATCH] Deployment interface --- src/app/api/deploy/route.ts | 20 ------------------- src/app/api/get-deployments/route.ts | 29 ++++++++++++++++++++++++++++ src/components/dialogs/DeployApp.tsx | 3 ++- src/core/deployment.ts | 1 - src/lib/dockship/application.ts | 10 ++++++++++ src/lib/dockship/deploy.ts | 15 ++++++++++++-- 6 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 src/app/api/get-deployments/route.ts diff --git a/src/app/api/deploy/route.ts b/src/app/api/deploy/route.ts index 1297a7d..84cba7a 100644 --- a/src/app/api/deploy/route.ts +++ b/src/app/api/deploy/route.ts @@ -3,26 +3,6 @@ import testAuth from "../auth"; import { StatusCodes } from "http-status-codes"; import { deployApp, getDeployments } from "@/core/deployment"; -export async function GET(req: NextRequest): Promise { - const auth = await testAuth(req); - - if (auth) { - return NextResponse.json( - { message: auth }, - { status: StatusCodes.UNAUTHORIZED } - ); - } - - // const apps = await getApps(); - - const apps = await getDeployments([]); - - return NextResponse.json({ - message: "ok", - apps, - }); -} - export async function POST(req: NextRequest): Promise { const auth = await testAuth(req); const { name, appId }: { name: string; appId: string } = await req.json(); diff --git a/src/app/api/get-deployments/route.ts b/src/app/api/get-deployments/route.ts new file mode 100644 index 0000000..3ebd933 --- /dev/null +++ b/src/app/api/get-deployments/route.ts @@ -0,0 +1,29 @@ +import { NextRequest, NextResponse } from "next/server"; +import testAuth from "../auth"; +import { StatusCodes } from "http-status-codes"; +import { deployApp, getDeployments } from "@/core/deployment"; + +export async function POST(req: NextRequest): Promise { + const auth = await testAuth(req); + const apps = await req.json(); + + if (auth) { + return NextResponse.json( + { message: auth }, + { status: StatusCodes.UNAUTHORIZED } + ); + } + + if (!apps) + return NextResponse.json( + { message: "Body should be Application[]" }, + { status: StatusCodes.UNAUTHORIZED } + ); + + const deployments = await getDeployments(apps); + + return NextResponse.json({ + message: "ok", + deployments, + }); +} diff --git a/src/components/dialogs/DeployApp.tsx b/src/components/dialogs/DeployApp.tsx index 764a2f5..3a6fede 100644 --- a/src/components/dialogs/DeployApp.tsx +++ b/src/components/dialogs/DeployApp.tsx @@ -18,6 +18,7 @@ import { Combobox } from "../ui/combobox"; import { deployApp } from "@/lib/dockship/deploy"; import { useAsync } from "@/hooks/use-async"; import { Application } from "@/lib/types"; +import { getApps } from "@/lib/dockship/application"; export default function DeployAppDialog({ defaultApp, @@ -27,7 +28,7 @@ export default function DeployAppDialog({ children: ReactNode; }) { const { value: nodes } = useAsync([], getLiveNodes); - const { value: apps } = useAsync([], async () => []); + const { value: apps } = useAsync([], getApps); const [name, setName] = useState(""); const [app, setApp] = useState(defaultApp || ""); const [node, setNode] = useState(""); diff --git a/src/core/deployment.ts b/src/core/deployment.ts index cd7c007..6342f5f 100644 --- a/src/core/deployment.ts +++ b/src/core/deployment.ts @@ -20,7 +20,6 @@ function sanitizeString(str: string): string { export async function getDeployments( apps: Application[] ): Promise { - // Limit concurrent Docker calls (adjust if needed) const limit = pLimit(5); const deploymentIds = apps.flatMap((app) => app.deployments); diff --git a/src/lib/dockship/application.ts b/src/lib/dockship/application.ts index c7049c3..064f8ed 100644 --- a/src/lib/dockship/application.ts +++ b/src/lib/dockship/application.ts @@ -13,6 +13,16 @@ export async function getApps(): Promise { return read(() => [], "data", "apps.json"); } +export async function getApp(appId: string): Promise { + if (await verifySession()) { + throw new Error("Unauthorized"); + } + + return (await read(() => [], "data", "apps.json")).find( + (app) => app.id === appId + ); +} + export async function registerApp(name: string, repo: string) { if (await verifySession()) { throw new Error("Unauthorized"); diff --git a/src/lib/dockship/deploy.ts b/src/lib/dockship/deploy.ts index 37c27b3..d2df6cf 100644 --- a/src/lib/dockship/deploy.ts +++ b/src/lib/dockship/deploy.ts @@ -3,16 +3,18 @@ import { Deployment } from "@/lib/types"; import { getNodes } from "./node"; import axios from "axios"; +import { editApp, getApp, getApps } from "./application"; export async function getDeployments(): Promise<(null | Deployment)[]> { const nodes = await getNodes(); + const apps = await getApps(); return ( await Promise.all( nodes.map( async (n) => await ( - await axios.get(`http://${n.ip}:3000/api/deploy`, { + await axios.post(`http://${n.ip}:3000/api/get-deployments`, apps, { headers: { Authorization: `ApiKey ${n.key}` }, }) ).data.apps @@ -32,7 +34,7 @@ export async function deployApp( if (!node) throw new Error("Node not found"); - return await ( + const deployId = await ( await axios.post( `http://${node.ip}:3000/api/deploy`, { name, appId }, @@ -41,4 +43,13 @@ export async function deployApp( } ) ).data.deployId; + + const app = await getApp(appId); + if (!app) throw new Error("(deployId) App not found"); + + app.deployments.push(deployId); + + editApp(appId, app); + + return deployId; }