39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { createRoom } from '@/lib/rooms';
|
|
import { rateLimit, getClientIdentifier } from '@/lib/rate-limit';
|
|
import { z } from 'zod';
|
|
|
|
const createRoomSchema = z.object({
|
|
teamMode: z.enum(['1', '2']).optional().default('2'),
|
|
});
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
const clientId = getClientIdentifier(req);
|
|
const rateLimitResult = rateLimit(`create:${clientId}`, 5, 3600000);
|
|
|
|
if (!rateLimitResult.success) {
|
|
return NextResponse.json(
|
|
{ error: 'Too many rooms created. Try again later.' },
|
|
{ status: 429 }
|
|
);
|
|
}
|
|
|
|
const body = await req.json().catch(() => ({}));
|
|
const { teamMode } = createRoomSchema.parse(body);
|
|
|
|
const { code, moderatorId } = createRoom(parseInt(teamMode) as 1 | 2);
|
|
|
|
return NextResponse.json({
|
|
code,
|
|
moderatorId,
|
|
remaining: rateLimitResult.remaining
|
|
});
|
|
} catch (error) {
|
|
console.error('Create room error:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to create room' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
} |