- Lead with purpose and exact local/CI commands. - Remove local absolute paths, stale auth notes and unverified claims. - Keep comments to the reason or invariant they protect.
56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
import { apiForward, isMockMode } from '../utils/api'
|
|
import { createMockTrainJob } from '../utils/train-jobs'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
if (isMockMode()) {
|
|
let model: 'small' | 'forecast' = 'small'
|
|
let filename: string | null = null
|
|
|
|
const contentType = getRequestHeader(event, 'content-type') ?? ''
|
|
if (contentType.includes('multipart/form-data')) {
|
|
const parts = await readMultipartFormData(event)
|
|
for (const part of parts ?? []) {
|
|
if (part.name === 'model' && part.data) {
|
|
const value = part.data.toString()
|
|
if (value === 'small' || value === 'forecast') model = value
|
|
}
|
|
if (part.name === 'file' && part.filename) {
|
|
filename = part.filename
|
|
}
|
|
}
|
|
} else {
|
|
const body = await readBody<{ model?: 'small' | 'forecast'; path?: string }>(event).catch(() => null)
|
|
if (body?.model === 'small' || body?.model === 'forecast') model = body.model
|
|
if (body?.path) filename = body.path
|
|
}
|
|
|
|
const job = createMockTrainJob(model, filename)
|
|
setResponseStatus(event, 202)
|
|
return {
|
|
job_id: job.job_id,
|
|
status: job.status,
|
|
model: job.model,
|
|
submitted_at: job.started_at,
|
|
mock: true,
|
|
}
|
|
}
|
|
|
|
// Forward the raw multipart CSV to Go; control credentials stay in this process.
|
|
const contentType = getRequestHeader(event, 'content-type') ?? ''
|
|
if (!contentType) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Content-Type must be application/json or multipart/form-data',
|
|
})
|
|
}
|
|
|
|
const body = await readRawBody(event)
|
|
return apiForward(event, '/api/train', {
|
|
method: 'POST',
|
|
body,
|
|
headers: { 'content-type': contentType },
|
|
control: true,
|
|
timeoutMs: 60_000,
|
|
})
|
|
})
|