mirror of
https://github.com/SirBlobby/Hoya26.git
synced 2026-02-04 03:34:34 -05:00
Docker Update and Fixes
This commit is contained in:
@@ -9,18 +9,80 @@ const __dirname = path.dirname(__filename);
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
// Backend API URL - use Docker service name or environment variable
|
||||
const API_URL = process.env.API_URL || 'http://api:5000';
|
||||
|
||||
// Enable compression
|
||||
app.use(compression());
|
||||
|
||||
// Parse JSON bodies for proxied requests
|
||||
app.use(express.json({ limit: '50mb' }));
|
||||
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
|
||||
|
||||
/**
|
||||
* API Proxy middleware
|
||||
* Forwards all /api/* requests to the backend service
|
||||
*/
|
||||
app.use('/api', async (req, res) => {
|
||||
const targetUrl = `${API_URL}${req.originalUrl}`;
|
||||
|
||||
try {
|
||||
const fetchOptions = {
|
||||
method: req.method,
|
||||
headers: {
|
||||
'Content-Type': req.get('Content-Type') || 'application/json',
|
||||
'Accept': req.get('Accept') || 'application/json',
|
||||
},
|
||||
};
|
||||
|
||||
// Forward body for POST/PUT/PATCH requests
|
||||
if (['POST', 'PUT', 'PATCH'].includes(req.method) && req.body) {
|
||||
fetchOptions.body = JSON.stringify(req.body);
|
||||
}
|
||||
|
||||
const response = await fetch(targetUrl, fetchOptions);
|
||||
|
||||
// Get content type from response
|
||||
const contentType = response.headers.get('content-type');
|
||||
|
||||
// Set response headers
|
||||
res.status(response.status);
|
||||
if (contentType) {
|
||||
res.set('Content-Type', contentType);
|
||||
}
|
||||
|
||||
// Handle different response types
|
||||
if (contentType && (contentType.includes('application/pdf') ||
|
||||
contentType.includes('text/plain') ||
|
||||
contentType.includes('application/octet-stream'))) {
|
||||
// Binary/file responses
|
||||
const buffer = await response.arrayBuffer();
|
||||
res.send(Buffer.from(buffer));
|
||||
} else {
|
||||
// JSON responses
|
||||
const data = await response.text();
|
||||
res.send(data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`API Proxy Error [${req.method} ${req.originalUrl}]:`, error.message);
|
||||
res.status(502).json({
|
||||
status: 'error',
|
||||
message: 'Failed to connect to backend service',
|
||||
details: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Serve static files from build directory
|
||||
const buildPath = path.join(__dirname, '../build');
|
||||
app.use(express.static(buildPath));
|
||||
|
||||
|
||||
app.get(/.*/, (req, res) => {
|
||||
// SPA fallback - serve index.html for all other routes
|
||||
app.get('*', (req, res) => {
|
||||
res.sendFile(path.join(buildPath, 'index.html'));
|
||||
});
|
||||
|
||||
app.listen(PORT, '0.0.0.0', () => {
|
||||
console.log(`Server is running on http://localhost:${PORT}`);
|
||||
console.log(`Frontend server running on http://localhost:${PORT}`);
|
||||
console.log(`API proxy target: ${API_URL}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user