-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (56 loc) · 1.9 KB
/
Copy pathserver.js
File metadata and controls
62 lines (56 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const distPath = path.join(__dirname, 'dist');
const API_BASE =
process.env.AI_BUILDER_BASE_URL ||
process.env.VITE_AI_BUILDER_BASE_URL ||
'https://space.ai-builders.com/backend';
const API_TOKEN = process.env.AI_BUILDER_TOKEN || process.env.VITE_AI_BUILDER_TOKEN;
const app = express();
app.use(express.static(distPath, {
maxAge: '1y',
etag: true,
setHeaders: (res, filePath) => {
if (filePath.endsWith('index.html')) {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
}
},
}));
app.use('/api', async (req, res) => {
try {
const targetUrl = API_BASE + req.originalUrl.replace(/^\/api/, '');
const headers = { ...req.headers };
delete headers.host;
delete headers.connection;
if (API_TOKEN) {
headers.authorization = `Bearer ${API_TOKEN}`;
}
const upstream = await fetch(targetUrl, {
method: req.method,
headers,
body: req.method === 'GET' || req.method === 'HEAD' ? undefined : req,
duplex: 'half',
});
res.status(upstream.status);
upstream.headers.forEach((value, key) => {
if (key.toLowerCase() === 'transfer-encoding') return;
res.setHeader(key, value);
});
const buffer = Buffer.from(await upstream.arrayBuffer());
res.send(buffer);
} catch (err) {
console.error('Proxy error', err);
res.status(502).json({ error: 'proxy_error', message: 'Failed to reach backend' });
}
});
app.get('*', (_req, res) => {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.sendFile(path.join(distPath, 'index.html'));
});
const port = Number(process.env.PORT || 8000);
app.listen(port, '0.0.0.0', () => {
console.log(`FlowPaste server running on http://0.0.0.0:${port}`);
});