Files
Brancheneinstufung2/gtm-architect/server.cjs
Floke 302a211239 feat(gtm-architect): Finalize migration and implement web scraping
- Refactors the gtm-architect Dockerfile for a flat, more efficient build process.
- Implements robust web scraping via BeautifulSoup in helpers.py for URL analysis in phase1.
- Makes shared library imports (gspread, pandas, etc.) in helpers.py optional to prevent ModuleNotFoundErrors in microservices.
- Implements the main execution logic in the orchestrator to handle command-line arguments.
- Updates documentation to reflect the new architecture, scraping feature, and dependency handling.
2026-01-03 08:43:53 +00:00

94 lines
2.7 KiB
JavaScript

const express = require('express');
const { spawn } = require('child_process');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const VERSION = "1.0.0"; // Added for debugging and tracking
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'));
});
}
const server = app.listen(port, () => {
console.log(`Server listening on port ${port} (Version: ${VERSION})`);
});
server.setTimeout(600000); // 10 minutes
server.keepAliveTimeout = 610000;
server.headersTimeout = 620000;