feat: add Redis-backed SSE updates
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
||||
import {
|
||||
getAdminDiscordIds,
|
||||
getSessionDiscordId,
|
||||
} from "@/lib/auth";
|
||||
import { isSseTopic, sse } from "@/lib/sse";
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ topic: string }> },
|
||||
) {
|
||||
const { topic } = await params;
|
||||
|
||||
if (!isSseTopic(topic)) {
|
||||
return new Response("Invalid SSE topic", { status: 404 });
|
||||
}
|
||||
|
||||
const endpoint = sse[topic];
|
||||
if (endpoint.adminOnly) {
|
||||
const session = await getServerSession(authOptions);
|
||||
const discordId = getSessionDiscordId(session);
|
||||
if (!discordId || !getAdminDiscordIds().includes(discordId)) {
|
||||
return new Response("Unauthorized", { status: 401 });
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return await endpoint.stream({ signal: request.signal });
|
||||
} catch (error) {
|
||||
console.error(`Failed to open SSE topic ${topic}`, error);
|
||||
return new Response("SSE unavailable", { status: 503 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user