Loading files
import { NextResponse } from "next/server";
import { AccessToken } from "livekit-server-sdk";
import { isArmcadeOffline, isLiveKitConfigured } from "@/lib/config";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
/**
* Mints a short-lived OPERATOR token for the teleop viewer (/control), signed with the
* Pi's self-hosted LiveKit key/secret from LIVEKIT_* env vars. This is what replaces the old
* "paste a token into the HTML" flow — the page fetches { url, token } and connects.
*
* The token grants data publish (to drive + join the queue) and subscribe (to watch the
* cameras) but NOT media publish — only the sim/robot publishes video. The queue is keyed on
* the token identity, so identity === the player's chosen handle.
*/
export async function GET(req: Request) {
if (!isLiveKitConfigured()) {
// "offair" = the ARMCADE_OFFLINE kill switch (deliberate downtime, public-friendly copy);
// "demo" = LIVEKIT_* creds simply missing (local dev). The client shows different copy per reason.
return NextResponse.json(
{ configured: false, reason: isArmcadeOffline() ? "offair" : "demo" },
{ status: 503 },
);
}
const identity = (new URL(req.url).searchParams.get("identity") || "operator").trim();
if (!identity) {
return NextResponse.json({ error: "missing identity" }, { status: 400 });
}
const room = process.env.LIVEKIT_ROOM!;
const at = new AccessToken(
process.env.LIVEKIT_API_KEY!,
process.env.LIVEKIT_API_SECRET!,
{ identity, name: identity, ttl: "12h" },
);
at.addGrant({
roomJoin: true,
room,
canPublish: false,
canSubscribe: true,
canPublishData: true,
});
return NextResponse.json({
url: process.env.LIVEKIT_URL,
token: await at.toJwt(),
room,
identity,
});
}
05241c15dThis file is pinned to the selected release. Download the original to open it in your design tool.