feat(backend): add foundation services and sql idempotency
This commit is contained in:
141
backend/core-api/src/routes/core.js
Normal file
141
backend/core-api/src/routes/core.js
Normal file
@@ -0,0 +1,141 @@
|
||||
import { Router } from 'express';
|
||||
import multer from 'multer';
|
||||
import { z } from 'zod';
|
||||
import { AppError } from '../lib/errors.js';
|
||||
import { requireAuth, requirePolicy } from '../middleware/auth.js';
|
||||
import { createSignedUrlSchema } from '../contracts/core/create-signed-url.js';
|
||||
import { invokeLlmSchema } from '../contracts/core/invoke-llm.js';
|
||||
|
||||
const DEFAULT_MAX_FILE_BYTES = 10 * 1024 * 1024;
|
||||
const ALLOWED_FILE_TYPES = new Set(['application/pdf', 'image/jpeg', 'image/jpg', 'image/png']);
|
||||
|
||||
const upload = multer({
|
||||
storage: multer.memoryStorage(),
|
||||
limits: {
|
||||
fileSize: Number(process.env.MAX_UPLOAD_BYTES || DEFAULT_MAX_FILE_BYTES),
|
||||
},
|
||||
});
|
||||
|
||||
const uploadMetaSchema = z.object({
|
||||
category: z.string().max(80).optional(),
|
||||
visibility: z.enum(['public', 'private']).optional(),
|
||||
});
|
||||
|
||||
function mockSignedUrl(fileUri, expiresInSeconds) {
|
||||
const encoded = encodeURIComponent(fileUri);
|
||||
const expiresAt = new Date(Date.now() + expiresInSeconds * 1000).toISOString();
|
||||
return {
|
||||
signedUrl: `https://storage.googleapis.com/mock-signed-url/${encoded}?expires=${expiresInSeconds}`,
|
||||
expiresAt,
|
||||
};
|
||||
}
|
||||
|
||||
function parseBody(schema, body) {
|
||||
const parsed = schema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
throw new AppError('VALIDATION_ERROR', 'Invalid request payload', 400, {
|
||||
issues: parsed.error.issues,
|
||||
});
|
||||
}
|
||||
return parsed.data;
|
||||
}
|
||||
|
||||
async function handleUploadFile(req, res, next) {
|
||||
try {
|
||||
const file = req.file;
|
||||
if (!file) {
|
||||
throw new AppError('INVALID_FILE', 'Missing file in multipart form data', 400);
|
||||
}
|
||||
|
||||
if (!ALLOWED_FILE_TYPES.has(file.mimetype)) {
|
||||
throw new AppError('INVALID_FILE_TYPE', `Unsupported file type: ${file.mimetype}`, 400);
|
||||
}
|
||||
|
||||
const maxFileSize = Number(process.env.MAX_UPLOAD_BYTES || DEFAULT_MAX_FILE_BYTES);
|
||||
if (file.size > maxFileSize) {
|
||||
throw new AppError('FILE_TOO_LARGE', `File exceeds ${maxFileSize} bytes`, 400);
|
||||
}
|
||||
|
||||
const meta = parseBody(uploadMetaSchema, req.body || {});
|
||||
const visibility = meta.visibility || 'private';
|
||||
const bucket = visibility === 'public'
|
||||
? process.env.PUBLIC_BUCKET || 'krow-workforce-dev-public'
|
||||
: process.env.PRIVATE_BUCKET || 'krow-workforce-dev-private';
|
||||
|
||||
const safeName = file.originalname.replace(/[^a-zA-Z0-9._-]/g, '_');
|
||||
const objectPath = `uploads/${req.actor.uid}/${Date.now()}_${safeName}`;
|
||||
|
||||
res.status(200).json({
|
||||
fileUri: `gs://${bucket}/${objectPath}`,
|
||||
contentType: file.mimetype,
|
||||
size: file.size,
|
||||
bucket,
|
||||
path: objectPath,
|
||||
requestId: req.requestId,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error?.code === 'LIMIT_FILE_SIZE') {
|
||||
return next(new AppError('FILE_TOO_LARGE', 'File exceeds upload limit', 400));
|
||||
}
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCreateSignedUrl(req, res, next) {
|
||||
try {
|
||||
const payload = parseBody(createSignedUrlSchema, req.body || {});
|
||||
const expiresInSeconds = payload.expiresInSeconds || 300;
|
||||
|
||||
const signed = mockSignedUrl(payload.fileUri, expiresInSeconds);
|
||||
|
||||
res.status(200).json({
|
||||
...signed,
|
||||
requestId: req.requestId,
|
||||
});
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleInvokeLlm(req, res, next) {
|
||||
try {
|
||||
const payload = parseBody(invokeLlmSchema, req.body || {});
|
||||
|
||||
if (process.env.LLM_MOCK === 'false') {
|
||||
throw new AppError('MODEL_FAILED', 'Real model integration not wired yet', 501);
|
||||
}
|
||||
|
||||
const startedAt = Date.now();
|
||||
res.status(200).json({
|
||||
result: {
|
||||
summary: 'Mock model response. Replace with Vertex AI integration.',
|
||||
inputPromptSize: payload.prompt.length,
|
||||
},
|
||||
model: process.env.LLM_MODEL || 'vertexai/gemini-mock',
|
||||
latencyMs: Date.now() - startedAt,
|
||||
requestId: req.requestId,
|
||||
});
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
|
||||
export function createCoreRouter() {
|
||||
const router = Router();
|
||||
|
||||
router.post('/upload-file', requireAuth, requirePolicy('core.upload', 'file'), upload.single('file'), handleUploadFile);
|
||||
router.post('/create-signed-url', requireAuth, requirePolicy('core.sign-url', 'file'), handleCreateSignedUrl);
|
||||
router.post('/invoke-llm', requireAuth, requirePolicy('core.invoke-llm', 'model'), handleInvokeLlm);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
export function createLegacyCoreRouter() {
|
||||
const router = Router();
|
||||
|
||||
router.post('/uploadFile', requireAuth, requirePolicy('core.upload', 'file'), upload.single('file'), handleUploadFile);
|
||||
router.post('/createSignedUrl', requireAuth, requirePolicy('core.sign-url', 'file'), handleCreateSignedUrl);
|
||||
router.post('/invokeLLM', requireAuth, requirePolicy('core.invoke-llm', 'model'), handleInvokeLlm);
|
||||
|
||||
return router;
|
||||
}
|
||||
12
backend/core-api/src/routes/health.js
Normal file
12
backend/core-api/src/routes/health.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Router } from 'express';
|
||||
|
||||
export const healthRouter = Router();
|
||||
|
||||
healthRouter.get('/healthz', (req, res) => {
|
||||
res.status(200).json({
|
||||
ok: true,
|
||||
service: 'krow-core-api',
|
||||
version: process.env.SERVICE_VERSION || 'dev',
|
||||
requestId: req.requestId,
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user