61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
const express = require('express');
|
|
const path = require('path');
|
|
const { GoogleAuth } = require('google-auth-library');
|
|
|
|
const app = express();
|
|
app.use(express.json({ limit: '1mb' }));
|
|
|
|
const BACKEND_URL = process.env.BACKEND_URL;
|
|
if (!BACKEND_URL) {
|
|
console.error('FATAL: BACKEND_URL env var is required');
|
|
process.exit(1);
|
|
}
|
|
|
|
const auth = new GoogleAuth();
|
|
|
|
app.get('/health', (_req, res) => {
|
|
res.status(200).json({ status: 'ok' });
|
|
});
|
|
|
|
app.post('/api/chat', async (req, res) => {
|
|
console.log('🔥🔥🔥 /api/chat ROUTE HIT 🔥🔥🔥');
|
|
try {
|
|
// Create an ID-token authenticated client for the backend (audience = BACKEND_URL)
|
|
const idTokenClient = await auth.getIdTokenClient(BACKEND_URL);
|
|
|
|
// Forward request to backend
|
|
const backendResp = await idTokenClient.request({
|
|
url: `${BACKEND_URL}/chat`,
|
|
method: 'POST',
|
|
data: req.body,
|
|
});
|
|
|
|
res.status(backendResp.status).json(backendResp.data);
|
|
} catch (err) {
|
|
console.error('Proxy error FULL:', {
|
|
message: err.message,
|
|
responseStatus: err.response?.status,
|
|
responseData: err.response?.data,
|
|
stack: err.stack,
|
|
});
|
|
const status = err.response?.status || 500;
|
|
const message = err.response?.data || { error: err.message };
|
|
res.status(err.response?.status || 500).json({
|
|
error: err.response?.data || err.message || 'Unknown proxy error',
|
|
});
|
|
}
|
|
});
|
|
|
|
// Serve static assets
|
|
app.use(express.static(path.join(__dirname, 'dist')));
|
|
|
|
// SPA fallback
|
|
app.get('*', (_req, res) => {
|
|
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
|
|
});
|
|
|
|
const port = parseInt(process.env.PORT || '8080', 10);
|
|
app.listen(port, '0.0.0.0', () => {
|
|
console.log(`Frontend proxy listening on port ${port}`);
|
|
});
|