Loading files
d2bd4d016This file is pinned to the selected release. Download the original to open it in your design tool.
import { NextResponse } from "next/server";
import { isAdminAuthorized, isAdminConfigured, sendAdminCommand } from "@/lib/admin";
import type { AdminAction, AdminTarget } from "@/lib/types";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
const TARGETS: AdminTarget[] = ["robots", "cameras"];
const ACTIONS: AdminAction[] = ["pause", "resume", "pause_after_session"];
/**
* Password-gated admin command → forwarded to the Pi bridge over LiveKit.
* curl -X POST /api/admin/command -H "x-admin-password: <pw>" \
* -H "content-type: application/json" -d '{"target":"robots","action":"pause"}'
*/
export async function POST(req: Request) {
if (!isAdminAuthorized(req)) {
return NextResponse.json({ error: "unauthorized" }, { status: 401 });
}
if (!isAdminConfigured()) {
return NextResponse.json({ error: "not configured — set LIVEKIT_* env" }, { status: 503 });
}
const body = (await req.json().catch(() => ({}))) as { target?: string; action?: string };
const target = body.target as AdminTarget;
const action = body.action as AdminAction;
if (!TARGETS.includes(target) || !ACTIONS.includes(action)) {
return NextResponse.json({ error: "bad target/action" }, { status: 400 });
}
// pause_after_session is only meaningful for the session-scoped target
if (action === "pause_after_session" && target !== "robots") {
return NextResponse.json({ error: "pause_after_session only supported for robots" }, { status: 400 });
}
try {
await sendAdminCommand({ target, action });
return NextResponse.json({ ok: true, target, action });
} catch (err) {
console.error("[admin] send failed", err);
return NextResponse.json({ error: "send failed" }, { status: 502 });
}
}