Files
Brancheneinstufung2/gtm-architect/server.cjs
Floke df58878973 fix(gtm-architect): Stabilize Docker environment and fix frontend
- Refactor Docker build process for gtm-app to ensure reliability
- Correct volume mounts and build context in docker-compose.yml to prevent stale code
- Fix frontend 404 error by using relative paths in index.html
- Ensure API key is correctly mounted into the container
- Stabilize Node.js to Python data passing via explicit --data argument
- Update gtm_architect_documentation.md with extensive troubleshooting guide
2026-01-01 08:36:48 +00:00

94 lines
3.3 KiB
JavaScript

console.log("--- GTM Architect Server starting ---");
try {
const express = require('express');
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3005; // Port for the GTM Architect service
app.use(express.json({ limit: '50mb' }));
// Determine Environment and Paths
const distPath = path.join(__dirname, 'dist');
const isProduction = fs.existsSync(distPath);
const staticDir = isProduction ? distPath : __dirname;
console.log(`[Init] Serving static files from: ${staticDir}`);
app.use(express.static(staticDir));
// Determine Python Script Path
// In Docker (optimized), script is in same dir. Locally, it might be one level up.
let pythonScriptPath = path.join(__dirname, 'gtm_architect_orchestrator.py');
if (!fs.existsSync(pythonScriptPath)) {
pythonScriptPath = path.join(__dirname, '../gtm_architect_orchestrator.py');
}
console.log(`[Init] Using Python script at: ${pythonScriptPath}`);
function callPythonScript(mode, data, res) {
const dataString = JSON.stringify(data);
const pythonProcess = spawn('python3', [pythonScriptPath, '--mode', mode, '--data', dataString]);
let pythonData = '';
let errorData = '';
pythonProcess.stdout.on('data', (data) => {
pythonData += data.toString();
});
pythonProcess.stderr.on('data', (data) => {
errorData += data.toString();
});
pythonProcess.on('close', (code) => {
if (code !== 0) {
console.error(`Python script exited with code ${code}`);
console.error('Stderr:', errorData);
return res.status(500).json({ error: 'Python script execution failed.', details: errorData });
}
try {
const result = JSON.parse(pythonData);
res.json(result);
} catch (e) {
console.error('Failed to parse Python script output:', e);
console.error('Raw output:', pythonData);
res.status(500).json({ error: 'Failed to parse Python script output.', details: pythonData });
}
});
}
// API endpoint to handle requests from the frontend
app.post('/api/gtm', (req, res) => {
const { mode, data } = req.body;
if (!mode || !data) {
return res.status(400).json({ error: 'Missing mode or data in request body' });
}
callPythonScript(mode, data, res);
});
// Serve the main index.html for any other requests to support client-side routing
app.get('*', (req, res) => {
res.sendFile(path.join(staticDir, 'index.html'));
});
const VERSION = "1.1.1_Fix_20260101_1200_FINAL"; // Add a version for debugging
const server = app.listen(port, () => {
console.log(`GTM Architect server listening at http://localhost:${port} (Version: ${VERSION})`);
});
// Prevent 502 Bad Gateway by increasing Node.js server timeouts to match Nginx
server.setTimeout(600000);
server.keepAliveTimeout = 610000;
server.headersTimeout = 620000;
} catch (e) {
console.error("!!! A CRITICAL ERROR OCCURRED ON STARTUP !!!");
console.error(e);
process.exit(1);
}