All 12 Next.js API routes that proxy requests to the ML service (INFERENCE_API_URL / localhost:8001) now include the X-API-Key header read from process.env.API_KEY. Affected routes: - /api/predict - /api/predict/batch - /api/model/info - /api/model/load - /api/training/start - /api/training/runs - /api/training/runs/[run_id] (DELETE) - /api/training/dataset-info - /api/training/active - /api/training/build-dataset - /api/patterns/available - /api/patterns/detect Marks task 3.3 as complete in openspec/changes/code-review-fix/tasks.md. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
const INFERENCE_API_URL = process.env.INFERENCE_API_URL || 'http://localhost:8001';
|
|
const INFERENCE_API_TIMEOUT = parseInt(process.env.INFERENCE_API_TIMEOUT || '30000', 10);
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
|
|
// Forward request to Python inference service
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(() => controller.abort(), INFERENCE_API_TIMEOUT);
|
|
|
|
try {
|
|
const response = await fetch(`${INFERENCE_API_URL}/predict`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-API-Key': process.env.API_KEY || '',
|
|
},
|
|
body: JSON.stringify(body),
|
|
signal: controller.signal,
|
|
});
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
// Forward the response from the inference service
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
return NextResponse.json(
|
|
{ error: data.detail || 'Prediction failed' },
|
|
{ status: response.status }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json(data);
|
|
} catch (fetchError: any) {
|
|
clearTimeout(timeoutId);
|
|
|
|
if (fetchError.name === 'AbortError') {
|
|
return NextResponse.json(
|
|
{ error: 'Prediction request timed out' },
|
|
{ status: 504 }
|
|
);
|
|
}
|
|
|
|
throw fetchError;
|
|
}
|
|
} catch (error: any) {
|
|
console.error('Predict proxy error:', error);
|
|
|
|
// Check if it's a connection error
|
|
if (error.cause?.code === 'ECONNREFUSED' || error.message?.includes('fetch failed')) {
|
|
return NextResponse.json(
|
|
{ error: 'Inference service unavailable' },
|
|
{ status: 503 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|