const express = require('express'); const { spawn } = require('child_process'); const cors = require('cors'); const path = require('path'); const fs = require('fs'); const app = express(); const port = 3005; // Enable CORS for all routes app.use(cors()); // Serve static files from the 'dist' directory const staticPath = path.join(__dirname, 'dist'); if (fs.existsSync(staticPath)) { app.use(express.static(staticPath)); } else { console.warn(`Static path not found: ${staticPath}. The frontend might not be served correctly. Make sure to build the frontend first.`); } app.use(express.json({ limit: '50mb' })); // API endpoint to run the Python script app.post('/api/run', (req, res) => { const { mode, ...payload } = req.body; if (!mode) { return res.status(400).json({ error: 'Mode is required' }); } // Convert payload to a JSON string and then to a Base64 string to ensure safe command line passing const payloadString = JSON.stringify(payload); const payloadBase64 = Buffer.from(payloadString).toString('base64'); const pythonScriptPath = path.join(__dirname, '../gtm_architect_orchestrator.py'); const pythonProcess = spawn('python3', [ pythonScriptPath, '--mode', mode, '--payload_base64', payloadBase64 ]); let stdoutData = ''; let stderrData = ''; pythonProcess.stdout.on('data', (data) => { stdoutData += data.toString(); }); pythonProcess.stderr.on('data', (data) => { stderrData += data.toString(); }); pythonProcess.on('close', (code) => { if (code !== 0) { console.error(`Python script exited with code ${code}`); console.error('Stderr:', stderrData); return res.status(500).json({ error: 'Python script execution failed.', stderr: stderrData, }); } try { // The Python script is expected to print JSON to stdout const result = JSON.parse(stdoutData); res.json(result); } catch (e) { console.error('Failed to parse JSON from Python script stdout:', e); console.error('Stdout:', stdoutData); res.status(500).json({ error: 'Failed to parse response from Python script.', stdout: stdoutData, }); } }); }); // Serve the main index.html for any other GET request to support client-side routing if (fs.existsSync(staticPath)) { app.get('*', (req, res) => { res.sendFile(path.join(staticPath, 'index.html')); }); } app.listen(port, () => { console.log(`Server listening on port ${port}`); });