Files
web-nuxt/server/api/train.post.ts
T
Noel Sabia 66b80a62fb
ci / check (push) Successful in 1m20s
ci / check (pull_request) Successful in 51s
docs: tighten README quick start and stale comments
- 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.
2026-11-14 09:16:19 +01:00

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,
})
})