diff --git a/b2b-marketing-assistant/server.cjs b/b2b-marketing-assistant/server.cjs index 98c474cf..947ef19e 100644 --- a/b2b-marketing-assistant/server.cjs +++ b/b2b-marketing-assistant/server.cjs @@ -16,6 +16,7 @@ app.use(bodyParser.json({ limit: '10mb' })); // Erhöhe das Limit für potenziel const PYTHON_EXECUTABLE = 'python3'; // Annahme, dass python3 im PATH des Containers ist // Im Docker-Container liegen server.cjs und das Python-Skript im selben Verzeichnis (/app) const SCRIPT_PATH = path.join(__dirname, 'b2b_marketing_orchestrator.py'); +const dbScript = '/app/market_db_manager.py'; // Absoluter Pfad zum DB Manager im Container // Helper-Funktion zum Ausführen des Python-Skripts @@ -166,10 +167,6 @@ app.post('/api/next-step', (req, res) => { // --- DATABASE ROUTES --- -// Initialize DB on startup -const dbScript = path.join(__dirname, 'market_db_manager.py'); -spawn('python3', [dbScript, 'init']); - app.get('/api/projects', (req, res) => { runPythonScript([dbScript, 'list'], res); }); @@ -191,13 +188,6 @@ app.post('/api/save-project', (req, res) => { try { fs.writeFileSync(tempFilePath, JSON.stringify(projectData)); runPythonScript([dbScript, 'save', tempFilePath], res); - // Clean up temp file - if (fs.existsSync(tempFilePath)) { - // Note: runPythonScript is async, so we might want to handle deletion there - // But since we are passing it to python which reads it, we'll let it be for now - // or pass it to runPythonScript cleanup if we had that. - // For now, I'll just leave it and let the user manage tmp if needed. - } } catch (e) { res.status(500).json({ error: 'Failed to write project data to disk' }); } @@ -212,7 +202,34 @@ app.get('*', (req, res) => { res.sendFile(path.join(__dirname, 'dist', 'index.html')); }); +// Separate function to initialize the database, called after server starts +const initializeDatabase = () => { + console.log(`[${new Date().toISOString()}] Initializing B2B database...`); + const pythonProcess = spawn(PYTHON_EXECUTABLE, [dbScript, 'init']); + + pythonProcess.stdout.on('data', (data) => { + console.log(`[DB Init STDOUT]: ${data.toString().trim()}`); + }); + + pythonProcess.stderr.on('data', (data) => { + console.error(`[DB Init STDERR]: ${data.toString().trim()}`); + }); + + pythonProcess.on('close', (code) => { + if (code === 0) { + console.log(`[${new Date().toISOString()}] B2B database initialized successfully.`); + } else { + console.error(`[${new Date().toISOString()}] B2B database initialization failed with code: ${code}`); + } + }); + + pythonProcess.on('error', (err) => { + console.error(`[${new Date().toISOString()}] Failed to spawn Python for DB init:`, err); + }); +}; + // Start des Servers app.listen(PORT, () => { console.log(`B2B Marketing Assistant API Bridge running on http://localhost:${PORT}`); -}); + initializeDatabase(); // Initialize DB after server is listening +}); \ No newline at end of file