- Add X-User-ID header containing user.id to all fetch calls from proxy routes - Updated routes: /api/predict, /api/predict/batch, /api/model/info, /api/model/load, /api/patterns/detect, /api/patterns/available, /api/training/start, /api/training/runs - Enables user scoping on the FastAPI ML service side Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { z } from 'zod';
|
|
import { getAuthUser } from '@/lib/auth';
|
|
|
|
const INFERENCE_API_URL = process.env.INFERENCE_API_URL || 'http://localhost:8001';
|
|
const INFERENCE_API_TIMEOUT = parseInt(process.env.INFERENCE_API_TIMEOUT || '30000', 10);
|
|
|
|
const ModelLoadRequestSchema = z.object({
|
|
run_id: z.string().min(1).regex(/^[a-zA-Z0-9_-]+$/, 'run_id must contain only alphanumeric characters, underscores, and hyphens'),
|
|
});
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const user = await getAuthUser();
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(() => controller.abort(), INFERENCE_API_TIMEOUT);
|
|
|
|
try {
|
|
const body = await request.json();
|
|
|
|
const result = ModelLoadRequestSchema.safeParse(body);
|
|
if (!result.success) {
|
|
clearTimeout(timeoutId);
|
|
return NextResponse.json(
|
|
{ error: 'Invalid request', details: result.error.flatten() },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const validatedBody = result.data;
|
|
|
|
const response = await fetch(`${INFERENCE_API_URL}/model/load`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', 'X-API-Key': process.env.API_KEY || '', 'X-User-ID': user.id },
|
|
body: JSON.stringify(validatedBody),
|
|
signal: controller.signal,
|
|
});
|
|
clearTimeout(timeoutId);
|
|
|
|
const data = await response.json();
|
|
if (!response.ok) {
|
|
return NextResponse.json({ error: data.detail || 'Failed to load model' }, { status: response.status });
|
|
}
|
|
return NextResponse.json(data);
|
|
} catch (error: any) {
|
|
clearTimeout(timeoutId);
|
|
if (error.name === 'AbortError') {
|
|
return NextResponse.json({ error: 'Model load timed out' }, { status: 504 });
|
|
}
|
|
if (error.cause?.code === 'ECONNREFUSED' || error.message?.includes('fetch failed')) {
|
|
return NextResponse.json({ error: 'Inference service unavailable' }, { status: 503 });
|
|
}
|
|
console.error('model/load proxy error:', error);
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|