Fix routing
This commit is contained in:
parent
4a2a9a7489
commit
44b6faaeb9
1 changed files with 37 additions and 15 deletions
|
|
@ -2,7 +2,9 @@ import express from 'express';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import helmet from 'helmet';
|
import helmet from 'helmet';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import fetch from 'node-fetch';
|
import http from 'node:http';
|
||||||
|
import https from 'node:https';
|
||||||
|
import { URL } from 'node:url';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
|
@ -20,24 +22,44 @@ const STORAGE_DIR = path.resolve(process.env.ASTRA_STORAGE || '/app/storage');
|
||||||
app.set('trust proxy', true);
|
app.set('trust proxy', true);
|
||||||
app.use(helmet({ contentSecurityPolicy: false }));
|
app.use(helmet({ contentSecurityPolicy: false }));
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(express.json());
|
|
||||||
|
|
||||||
app.get('/api/healthz', (_req, res) => {
|
app.get('/api/healthz', (_req, res) => {
|
||||||
res.json({ status: 'ok', upstream: RUST_ENGINE_BASE });
|
res.json({ status: 'ok', upstream: RUST_ENGINE_BASE });
|
||||||
});
|
});
|
||||||
|
|
||||||
// Proxy minimal API needed by the UI to the rust-engine container
|
// Proxy all /api/* calls (including POST bodies, multipart uploads, etc.)
|
||||||
app.post('/api/files/import-demo', async (req, res) => {
|
app.use('/api', (req, res) => {
|
||||||
try {
|
const targetUrl = new URL(req.originalUrl, RUST_ENGINE_BASE);
|
||||||
const qs = req.url.includes('?') ? req.url.substring(req.url.indexOf('?')) : '';
|
const client = targetUrl.protocol === 'https:' ? https : http;
|
||||||
const url = `${RUST_ENGINE_BASE}/api/files/import-demo${qs}`;
|
|
||||||
const upstream = await fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: req.body ? JSON.stringify(req.body) : undefined });
|
const headers = { ...req.headers, host: targetUrl.host };
|
||||||
const text = await upstream.text();
|
|
||||||
res.status(upstream.status).type(upstream.headers.get('content-type') || 'application/json').send(text);
|
const proxyReq = client.request(
|
||||||
} catch (err) {
|
targetUrl,
|
||||||
console.error('import-demo proxy failed:', err);
|
{
|
||||||
res.status(502).json({ error: 'proxy_failed' });
|
method: req.method,
|
||||||
}
|
headers,
|
||||||
|
},
|
||||||
|
(upstream) => {
|
||||||
|
res.status(upstream.statusCode || 502);
|
||||||
|
for (const [key, value] of Object.entries(upstream.headers)) {
|
||||||
|
if (typeof value !== 'undefined') {
|
||||||
|
res.setHeader(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
upstream.pipe(res);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
proxyReq.on('error', (err) => {
|
||||||
|
console.error('API proxy error:', err);
|
||||||
|
if (!res.headersSent) {
|
||||||
|
res.status(502).json({ error: 'proxy_failed' });
|
||||||
|
} else {
|
||||||
|
res.end();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
req.pipe(proxyReq);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Serve static frontend
|
// Serve static frontend
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue