import { NextRequest, NextResponse } from 'next/server'; import { executePython } from '@/lib/pistonClient'; import type { ExecuteRequestBody } from '@/types'; const MAX_CODE_LENGTH = 10_000; export async function POST(req: NextRequest) { let body: ExecuteRequestBody; try { body = await req.json(); } catch { return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 }); } const { language, code, stdin } = body; if (!language || !code) { return NextResponse.json({ error: 'Missing language or code' }, { status: 400 }); } if (code.length > MAX_CODE_LENGTH) { return NextResponse.json({ error: 'Code exceeds maximum length of 10,000 characters' }, { status: 400 }); } // HTML is rendered client-side via iframe — no backend execution needed if (language === 'html') { return NextResponse.json({ stdout: '', stderr: '', exitCode: 0, timedOut: false, }); } const result = await executePython(code, stdin ?? ''); return NextResponse.json(result); }