const { app, BrowserWindow } = require("electron"); const path = require("path"); const { spawn } = require("child_process"); const BACKEND_PORT = process.env.ADDRESSGEN_PORT || "8732"; const API_BASE = `http://127.0.0.1:${BACKEND_PORT}`; let backendProcess = null; function startBackend() { const env = { ...process.env, ADDRESSGEN_PORT: BACKEND_PORT, }; if (process.env.BACKEND_BIN) { backendProcess = spawn(process.env.BACKEND_BIN, [], { env, stdio: "inherit" }); return; } if (app.isPackaged) { const binPath = path.join(process.resourcesPath, "backend", "addressgen-backend"); backendProcess = spawn(binPath, [], { env, stdio: "inherit" }); } else { const python = process.env.PYTHON || "python3"; const script = path.join(__dirname, "..", "backend", "app.py"); backendProcess = spawn(python, [script], { env, stdio: "inherit" }); } } function createWindow() { process.env.ADDRESSGEN_API_BASE = API_BASE; const win = new BrowserWindow({ width: 1100, height: 720, webPreferences: { preload: path.join(__dirname, "preload.js"), contextIsolation: true, nodeIntegration: false, }, }); if (app.isPackaged) { win.loadFile(path.join(__dirname, "renderer", "index.html")); } else { win.loadURL("http://localhost:5173"); } } app.whenReady().then(() => { startBackend(); createWindow(); app.on("activate", () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); }); app.on("before-quit", () => { if (backendProcess) { backendProcess.kill(); } });