// @/app/api/stream/route.ts import { NextResponse } from "next/server"; /** * @openapi * components: * schemas: * StreamError: * type: object * properties: * success: * type: boolean * example: false * error: * type: string * example: "Fields incomplete!" */ let currentSong = { number: "", song: "", songbook: "", }; const clients = new Set(); /** * @openapi * /api/stream: * get: * summary: Aktuell gestreamtes Lied abrufen (Server-Sent Events) * description: Öffnet einen SSE-Stream. Beim Verbinden wird das aktuelle Lied gesendet (falls gesetzt), danach bei jeder Änderung ein neues Event. * tags: * - Stream * responses: * 200: * description: Event-Stream mit dem aktuellen Lied * content: * text/event-stream: * schema: * type: string * example: 'data: {"number":"104a","song":"Großer Gott","songbook":"Feiert Jesus 1"}' */ 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', }, }); } /** * @openapi * /api/stream: * post: * summary: Lied zum Streamen festlegen * description: Setzt das aktuelle Lied und sendet es an alle verbundenen Stream-Clients. * tags: * - Stream * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - song * - songBook * - number * properties: * song: * type: string * example: "Großer Gott" * songBook: * type: string * example: "Feiert Jesus 1" * number: * type: string * example: "104a" * responses: * 200: * description: Lied gesetzt und an alle Clients gesendet * content: * application/json: * schema: * type: object * properties: * success: * type: boolean * example: true * currentSong: * type: object * properties: * number: * type: string * example: "104a" * song: * type: string * example: "Großer Gott" * songbook: * type: string * example: "Feiert Jesus 1" * 400: * description: Felder unvollständig * content: * application/json: * schema: * $ref: '#/components/schemas/StreamError' * 500: * description: Serverfehler * content: * application/json: * schema: * $ref: '#/components/schemas/StreamError' */ 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 }; clients.forEach((controller) => { controller.enqueue(`data: ${JSON.stringify(currentSong)}\n\n`); }); return NextResponse.json({ success: true , currentSong }, { status: 200 }); } catch (error) { return NextResponse.json({ success: false, error: "Server-Fehler" }, { status: 500 }); } }