69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
// @wikiCategory: Stream
|
|
|
|
import { NextResponse } from "next/server";
|
|
import { getSongById } from "@/lib/db/song";
|
|
import { getSongBookById } from "@/lib/db/songbook";
|
|
|
|
|
|
let currentSong = {
|
|
number: "",
|
|
song: "",
|
|
songbook: "",
|
|
};
|
|
const clients = new Set<ReadableStreamDefaultController>();
|
|
|
|
|
|
// @wikiDescription: Akutell gestreamtes Lied abrufen
|
|
export async function GET() {
|
|
let controllerRef: ReadableStreamDefaultController;
|
|
|
|
const stream = new ReadableStream({
|
|
start(controller) {
|
|
controllerRef = controller;
|
|
clients.add(controller);
|
|
|
|
if (currentSong.song) {
|
|
controller.enqueue(`data: ${JSON.stringify(currentSong)}\n\n`);
|
|
}
|
|
|
|
},
|
|
cancel() {
|
|
clients.delete(controllerRef);
|
|
},
|
|
});
|
|
return new NextResponse(stream, {
|
|
headers: {
|
|
'Content-Type': 'text/event-stream',
|
|
'Cache-Control': 'no-cache',
|
|
'Connection': 'keep-alive',
|
|
},
|
|
});
|
|
}
|
|
|
|
// @wikiDescription: Lied zum Streamen festlegen
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const data = await request.json();
|
|
const song = data.song?.trim() ?? null;
|
|
const songbook = data.songBook?.trim() ?? null;
|
|
const number = data.number?.trim() ?? null;
|
|
|
|
if (song == null || songbook == null || number == null) {
|
|
return NextResponse.json({ success: false, error: "Fields incomplete!" }, { status: 400 });
|
|
}
|
|
|
|
currentSong = {
|
|
song,
|
|
songbook,
|
|
number
|
|
};
|
|
|
|
const payload = JSON.stringify(currentSong);
|
|
clients.forEach((send) => send(payload));
|
|
|
|
return NextResponse.json({ success: true , currentSong }, { status: 200 });
|
|
} catch (error) {
|
|
return NextResponse.json({ success: false, error: "Server-Fehler" }, { status: 500 });
|
|
}
|
|
}
|