diff --git a/src/app/api/admin/nodes/route.ts b/src/app/api/admin/nodes/route.ts new file mode 100644 index 0000000..d96503b --- /dev/null +++ b/src/app/api/admin/nodes/route.ts @@ -0,0 +1,32 @@ +import { type NextRequest, NextResponse } from "next/server"; +import { gatewayUrl } from "@/lib/api/gateway"; +import { getAccessToken } from "@/lib/auth/session"; +import { decodeJwt } from "@/lib/auth/jwt"; + +export const runtime = "nodejs"; + +/** Create a render node (POST body forwarded to the gateway). Admin-gated. */ +export async function POST(req: NextRequest) { + const token = await getAccessToken(); + if (!token) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + const claims = decodeJwt(token); + const isAdmin = + String(claims?.is_admin) === "true" || claims?.is_admin === true || String(claims?.is_tenant_admin) === "true"; + if (!isAdmin) return NextResponse.json({ error: "Forbidden" }, { status: 403 }); + + const body = await req.text(); + const res = await fetch(gatewayUrl("/v1/nodes"), { + method: "POST", + cache: "no-store", + headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` }, + body, + }); + const data = await res.json().catch(() => null); + if (!res.ok) { + return NextResponse.json( + { error: data?.message ?? data?.error?.message ?? "Gateway error" }, + { status: res.status } + ); + } + return NextResponse.json(data ?? { ok: true }); +} diff --git a/src/components/admin/NodesTable.tsx b/src/components/admin/NodesTable.tsx index 269413f..1e466a7 100644 --- a/src/components/admin/NodesTable.tsx +++ b/src/components/admin/NodesTable.tsx @@ -31,10 +31,19 @@ function heartbeatAge(iso: string): string { return `${Math.floor(diff / 3600)}h ago`; } +const AE_VERSIONS = ["2025", "2024", "2023", "2022", "2021", "2020"]; +const NODE_KINDS = ["Shared", "Dedicated", "Spot"]; +const emptyNode = { name: "", region: "", node_ip: "", worker_port: 8088, current_ae_version: "2024", node_kind: "Dedicated", ram_gb: "", cpu_cores: "", priority: 5 }; +const fldCls = "w-full rounded-lg border border-[#262b40] bg-[#0c0e1a] px-3 py-2 text-sm text-gray-100 outline-none focus:border-indigo-500"; + export function NodesTable({ nodes }: { nodes: V2Node[] }) { const t = useTranslations("auto.componentsAdminNodesTable"); const router = useRouter(); const [loading, setLoading] = useState>({}); + const [showAdd, setShowAdd] = useState(false); + const [nf, setNf] = useState({ ...emptyNode }); + const [saving, setSaving] = useState(false); + const [err, setErr] = useState(null); const action = async (nodeId: string, endpoint: string) => { setLoading((p) => ({ ...p, [nodeId]: true })); @@ -46,16 +55,83 @@ export function NodesTable({ nodes }: { nodes: V2Node[] }) { } }; + const addNode = async () => { + setSaving(true); setErr(null); + const res = await fetch("/api/admin/nodes", { + method: "POST", headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + name: nf.name, region: nf.region, node_ip: nf.node_ip, worker_port: Number(nf.worker_port), + current_ae_version: nf.current_ae_version, node_kind: nf.node_kind, + ram_gb: nf.ram_gb ? Number(nf.ram_gb) : null, cpu_cores: nf.cpu_cores ? Number(nf.cpu_cores) : null, + priority: Number(nf.priority) || 5, + }), + }); + const d = await res.json().catch(() => null); + if (res.ok) { setShowAdd(false); setNf({ ...emptyNode }); router.refresh(); } + else setErr(d?.error ?? "ساخت نود ناموفق بود"); + setSaving(false); + }; + + const addBtn = ( + + ); + + const addModal = showAdd && ( +
setShowAdd(false)}> +
e.stopPropagation()}> +
+

افزودن نود رندر

+ +
+
+ {err &&

{err}

} +
setNf({ ...nf, name: e.target.value })} />
+
setNf({ ...nf, region: e.target.value })} placeholder="ir-tehran" dir="ltr" />
+
setNf({ ...nf, node_ip: e.target.value })} placeholder="192.168.1.10" dir="ltr" />
+
setNf({ ...nf, worker_port: Number(e.target.value) })} dir="ltr" />
+
+ + +
+
+ + +
+
setNf({ ...nf, ram_gb: e.target.value })} dir="ltr" />
+
setNf({ ...nf, cpu_cores: e.target.value })} dir="ltr" />
+
setNf({ ...nf, priority: Number(e.target.value) })} dir="ltr" />
+
+
+ + +
+
+
+ ); + if (nodes.length === 0) { return ( -
- {t("emptyState")} +
+
{addBtn}
+
+ {t("emptyState")} +
+ {addModal}
); } return ( -
+
+
{addBtn}
+ {addModal} +
@@ -134,6 +210,7 @@ export function NodesTable({ nodes }: { nodes: V2Node[] }) { ))}
+
); }