feat(admin): add-node form + After Effects version dropdown
- Nodes page: "+ افزودن نود" opens a full-screen form (name, region, IP, worker port, AE version, node kind, RAM, CPU, priority) → POST /v1/nodes - current_ae_version is now a dropdown (2025…2020, matching the ae_version DB enum) instead of free text; node_kind is a dropdown (Shared/Dedicated/Spot) - new POST /api/admin/nodes proxy route (forwards body; admin-gated). The backend POST /v1/nodes existed but had no UI — you couldn't define nodes before. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
@@ -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<Record<string, boolean>>({});
|
||||
const [showAdd, setShowAdd] = useState(false);
|
||||
const [nf, setNf] = useState({ ...emptyNode });
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [err, setErr] = useState<string | null>(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 = (
|
||||
<button onClick={() => { setShowAdd(true); setErr(null); }} className="rounded-lg bg-indigo-600 px-4 py-2 text-sm font-medium text-white hover:bg-indigo-500">
|
||||
+ افزودن نود
|
||||
</button>
|
||||
);
|
||||
|
||||
const addModal = showAdd && (
|
||||
<div className="fixed inset-0 z-50 flex items-stretch justify-center bg-black/70 p-2 sm:p-6" dir="rtl" onClick={() => setShowAdd(false)}>
|
||||
<div className="flex max-h-full w-full max-w-2xl flex-col rounded-xl border border-[#1e2235] bg-[#0f1120]" onClick={(e) => e.stopPropagation()}>
|
||||
<div className="flex items-center justify-between border-b border-[#1e2235] px-5 py-3">
|
||||
<h2 className="text-sm font-semibold text-white">افزودن نود رندر</h2>
|
||||
<button className="rounded-lg px-2 py-1 text-gray-400 hover:bg-[#161a2e] hover:text-white" onClick={() => setShowAdd(false)}>✕</button>
|
||||
</div>
|
||||
<div className="grid flex-1 gap-3 overflow-y-auto p-5 sm:grid-cols-2">
|
||||
{err && <p className="sm:col-span-2 rounded-lg bg-red-500/10 px-3 py-2 text-sm text-red-300">{err}</p>}
|
||||
<div><label className="mb-1 block text-xs text-gray-400">نام نود *</label><input className={fldCls} value={nf.name} onChange={(e) => setNf({ ...nf, name: e.target.value })} /></div>
|
||||
<div><label className="mb-1 block text-xs text-gray-400">منطقه (Region) *</label><input className={fldCls} value={nf.region} onChange={(e) => setNf({ ...nf, region: e.target.value })} placeholder="ir-tehran" dir="ltr" /></div>
|
||||
<div><label className="mb-1 block text-xs text-gray-400">آدرس IP *</label><input className={fldCls} value={nf.node_ip} onChange={(e) => setNf({ ...nf, node_ip: e.target.value })} placeholder="192.168.1.10" dir="ltr" /></div>
|
||||
<div><label className="mb-1 block text-xs text-gray-400">پورت Worker *</label><input className={fldCls} type="number" value={nf.worker_port} onChange={(e) => setNf({ ...nf, worker_port: Number(e.target.value) })} dir="ltr" /></div>
|
||||
<div>
|
||||
<label className="mb-1 block text-xs text-gray-400">نسخهٔ افترافکت *</label>
|
||||
<select className={fldCls} value={nf.current_ae_version} onChange={(e) => setNf({ ...nf, current_ae_version: e.target.value })}>
|
||||
{AE_VERSIONS.map((v) => <option key={v} value={v}>After Effects {v}</option>)}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className="mb-1 block text-xs text-gray-400">نوع نود</label>
|
||||
<select className={fldCls} value={nf.node_kind} onChange={(e) => setNf({ ...nf, node_kind: e.target.value })}>
|
||||
{NODE_KINDS.map((k) => <option key={k} value={k}>{k}</option>)}
|
||||
</select>
|
||||
</div>
|
||||
<div><label className="mb-1 block text-xs text-gray-400">RAM (GB)</label><input className={fldCls} type="number" value={nf.ram_gb} onChange={(e) => setNf({ ...nf, ram_gb: e.target.value })} dir="ltr" /></div>
|
||||
<div><label className="mb-1 block text-xs text-gray-400">هستههای CPU</label><input className={fldCls} type="number" value={nf.cpu_cores} onChange={(e) => setNf({ ...nf, cpu_cores: e.target.value })} dir="ltr" /></div>
|
||||
<div><label className="mb-1 block text-xs text-gray-400">اولویت</label><input className={fldCls} type="number" value={nf.priority} onChange={(e) => setNf({ ...nf, priority: Number(e.target.value) })} dir="ltr" /></div>
|
||||
</div>
|
||||
<div className="flex items-center justify-end gap-2 border-t border-[#1e2235] px-5 py-3">
|
||||
<button className="rounded-lg border border-[#262b40] px-3 py-1.5 text-xs text-gray-300 hover:bg-[#161a2e]" onClick={() => setShowAdd(false)}>انصراف</button>
|
||||
<button className="rounded-lg bg-indigo-600 px-4 py-2 text-sm font-medium text-white hover:bg-indigo-500 disabled:opacity-50" onClick={addNode} disabled={saving || !nf.name || !nf.region || !nf.node_ip}>{saving ? "در حال ذخیره…" : "ذخیره نود"}</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
if (nodes.length === 0) {
|
||||
return (
|
||||
<div className="rounded-xl border border-[#1e2235] bg-[#0f1120] px-6 py-16 text-center text-sm text-gray-500">
|
||||
{t("emptyState")}
|
||||
<div className="space-y-3">
|
||||
<div className="flex justify-end">{addBtn}</div>
|
||||
<div className="rounded-xl border border-[#1e2235] bg-[#0f1120] px-6 py-16 text-center text-sm text-gray-500">
|
||||
{t("emptyState")}
|
||||
</div>
|
||||
{addModal}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="overflow-hidden rounded-xl border border-[#1e2235]">
|
||||
<div className="space-y-3">
|
||||
<div className="flex justify-end">{addBtn}</div>
|
||||
{addModal}
|
||||
<div className="overflow-hidden rounded-xl border border-[#1e2235]">
|
||||
<table className="w-full text-sm">
|
||||
<thead>
|
||||
<tr className="border-b border-[#1e2235] bg-[#0f1120] text-left text-xs font-medium uppercase tracking-wider text-gray-500">
|
||||
@@ -134,6 +210,7 @@ export function NodesTable({ nodes }: { nodes: V2Node[] }) {
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user