import { NextRequest, NextResponse } from 'next/server' /** * Proxy for the school-service (Go/Gin, port 8084). The browser cannot call * the backend directly because studio-v2 is served over HTTPS and the backend * is plain HTTP; this Next.js route bridges them server-side. */ const BACKEND_URL = process.env.SCHOOL_SERVICE_URL || 'http://school-service:8084' async function proxyRequest( request: NextRequest, params: { path: string[] } ): Promise { const path = params.path.join('/') const url = `${BACKEND_URL}/api/v1/school/${path}` try { const headers: HeadersInit = { 'Content-Type': 'application/json' } const authHeader = request.headers.get('authorization') if (authHeader) headers['Authorization'] = authHeader const fetchOptions: RequestInit = { method: request.method, headers, } if (['POST', 'PUT', 'PATCH'].includes(request.method)) { fetchOptions.body = await request.text() } const response = await fetch(url, fetchOptions) const contentType = response.headers.get('content-type') const data = contentType?.includes('application/json') ? await response.text() : await response.arrayBuffer() return new NextResponse(data, { status: response.status, headers: { 'Content-Type': contentType || 'application/json' }, }) } catch (error) { console.error(`Failed to proxy ${request.method} ${url}:`, error) return NextResponse.json( { error: 'school-service nicht erreichbar', details: error instanceof Error ? error.message : 'Unknown error' }, { status: 502 }, ) } } export async function GET(request: NextRequest, context: { params: Promise<{ path: string[] }> }) { return proxyRequest(request, await context.params) } export async function POST(request: NextRequest, context: { params: Promise<{ path: string[] }> }) { return proxyRequest(request, await context.params) } export async function PUT(request: NextRequest, context: { params: Promise<{ path: string[] }> }) { return proxyRequest(request, await context.params) } export async function DELETE(request: NextRequest, context: { params: Promise<{ path: string[] }> }) { return proxyRequest(request, await context.params) }