63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
const express = require('express');
|
|
const { spawn } = require('child_process');
|
|
const app = express();
|
|
const port = 3005; // Port for the GTM Architect service
|
|
|
|
app.use(express.json({ limit: '50mb' }));
|
|
|
|
// Middleware to serve static files from the React app
|
|
app.use(express.static('.'));
|
|
|
|
function callPythonScript(mode, data, res) {
|
|
const pythonProcess = spawn('python3', ['../gtm_architect_orchestrator.py', '--mode', mode]);
|
|
|
|
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 });
|
|
}
|
|
});
|
|
|
|
pythonProcess.stdin.write(JSON.stringify(data));
|
|
pythonProcess.stdin.end();
|
|
}
|
|
|
|
// 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(__dirname + '/index.html');
|
|
});
|
|
|
|
|
|
app.listen(port, () => {
|
|
console.log(`GTM Architect server listening at http://localhost:${port}`);
|
|
});
|