|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// ───────────────────────────────────────────────────────────────────────────── |
| 4 | +// Vagas Full – Electron main process |
| 5 | +// |
| 6 | +// Boot flow: |
| 7 | +// 1. Show loading window while the backend starts |
| 8 | +// 2. Import backend server in-process → Express starts on localhost:3001 |
| 9 | +// 3. Poll GET /api/health until the server accepts connections |
| 10 | +// 4. Open the main window (React app served by Express) |
| 11 | +// 5. Scraper execution is user-triggered from the UI via /api/scraper/run |
| 12 | +// |
| 13 | +// Why in-process import() instead of spawn('node', ...)? |
| 14 | +// spawn('node', ...) requires Node to be installed on the user's machine. |
| 15 | +// import() works because Electron ships its own Node runtime – the app |
| 16 | +// runs fully standalone, no external Node installation needed. |
| 17 | +// ───────────────────────────────────────────────────────────────────────────── |
| 18 | + |
| 19 | +const { app, BrowserWindow, dialog } = require('electron'); |
| 20 | +const path = require('path'); |
| 21 | +const http = require('http'); |
| 22 | +const { pathToFileURL } = require('url'); |
| 23 | + |
| 24 | +const BACKEND_PORT = 3001; |
| 25 | +const BACKEND_URL = `http://localhost:${BACKEND_PORT}`; |
| 26 | +const MAX_RETRIES = 60; // 60 × 500 ms = 30 s maximum wait |
| 27 | +const RETRY_INTERVAL_MS = 500; // ms between health-check polls |
| 28 | + |
| 29 | +// ─── Path helpers ───────────────────────────────────────────────────────────── |
| 30 | +// app.getAppPath() returns: |
| 31 | +// development : workspace root (where root package.json lives) |
| 32 | +// packaged : <install>/resources/app (asar: false) |
| 33 | + |
| 34 | +function getAppRootPath() { |
| 35 | + return app.getAppPath(); |
| 36 | +} |
| 37 | + |
| 38 | +function getFrontendDistPath() { |
| 39 | + return path.join(getAppRootPath(), 'frontend', 'dist'); |
| 40 | +} |
| 41 | + |
| 42 | +function getBackendServerPath() { |
| 43 | + return path.join(getAppRootPath(), 'backend', 'src', 'server.js'); |
| 44 | +} |
| 45 | + |
| 46 | +// userData is the OS user-data directory – writable even under Program Files, |
| 47 | +// and survives app updates. |
| 48 | +function getOutputDir() { |
| 49 | + return path.join(app.getPath('userData'), 'output'); |
| 50 | +} |
| 51 | + |
| 52 | +function getLoadingPagePath() { |
| 53 | + return path.join(getAppRootPath(), 'electron', 'loading.html'); |
| 54 | +} |
| 55 | + |
| 56 | +// ─── Backend startup (in-process) ───────────────────────────────────────────── |
| 57 | + |
| 58 | +async function startBackend() { |
| 59 | + // All env vars must be set BEFORE the ESM module is imported so they are |
| 60 | + // visible at module-evaluation time. |
| 61 | + const outDir = getOutputDir(); |
| 62 | + process.env.PORT = String(BACKEND_PORT); |
| 63 | + process.env.ELECTRON_OUTPUT_DIR = outDir; // API reads XLSX files from here |
| 64 | + process.env.ELECTRON_STATIC_DIR = getFrontendDistPath(); // Express serves the React build |
| 65 | + process.env.OUTPUT_FILE = path.join(outDir, 'vagas_linkedin.xlsx'); // scraper writes here |
| 66 | + process.env.PDF_FILE = path.join(outDir, 'vagas_linkedin.pdf'); // scraper writes here |
| 67 | + |
| 68 | + // pathToFileURL produces a correct file:// URL on Windows (handles spaces & backslashes) |
| 69 | + await import(pathToFileURL(getBackendServerPath()).href); |
| 70 | +} |
| 71 | + |
| 72 | +// ─── Health-check polling ────────────────────────────────────────────────────── |
| 73 | + |
| 74 | +function waitForBackend() { |
| 75 | + return new Promise((resolve, reject) => { |
| 76 | + let retries = 0; |
| 77 | + |
| 78 | + function check() { |
| 79 | + const req = http.get( |
| 80 | + `${BACKEND_URL}/api/health`, |
| 81 | + { timeout: 2000 }, |
| 82 | + (res) => { |
| 83 | + res.resume(); // drain body; only the status code matters |
| 84 | + if (res.statusCode === 200) { |
| 85 | + resolve(); |
| 86 | + } else { |
| 87 | + scheduleRetry(); |
| 88 | + } |
| 89 | + } |
| 90 | + ); |
| 91 | + |
| 92 | + req.on('error', scheduleRetry); |
| 93 | + req.on('timeout', () => { |
| 94 | + req.destroy(); |
| 95 | + scheduleRetry(); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + function scheduleRetry() { |
| 100 | + retries += 1; |
| 101 | + if (retries >= MAX_RETRIES) { |
| 102 | + reject( |
| 103 | + new Error( |
| 104 | + `O servidor backend não respondeu após ${MAX_RETRIES} tentativas (${(MAX_RETRIES * RETRY_INTERVAL_MS) / 1000}s).` |
| 105 | + ) |
| 106 | + ); |
| 107 | + return; |
| 108 | + } |
| 109 | + setTimeout(check, RETRY_INTERVAL_MS); |
| 110 | + } |
| 111 | + |
| 112 | + check(); |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +// ─── Windows ────────────────────────────────────────────────────────────────── |
| 117 | + |
| 118 | +function createLoadingWindow() { |
| 119 | + const win = new BrowserWindow({ |
| 120 | + width: 520, |
| 121 | + height: 360, |
| 122 | + resizable: false, |
| 123 | + frame: false, |
| 124 | + show: false, |
| 125 | + backgroundColor: '#0f172a', |
| 126 | + webPreferences: { |
| 127 | + contextIsolation: true, |
| 128 | + nodeIntegration: false, |
| 129 | + sandbox: true, |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + win.loadFile(getLoadingPagePath()); |
| 134 | + win.once('ready-to-show', () => win.show()); |
| 135 | + return win; |
| 136 | +} |
| 137 | + |
| 138 | +function createMainWindow() { |
| 139 | + const win = new BrowserWindow({ |
| 140 | + width: 1280, |
| 141 | + height: 800, |
| 142 | + minWidth: 900, |
| 143 | + minHeight: 600, |
| 144 | + show: false, |
| 145 | + backgroundColor: '#0f172a', |
| 146 | + title: 'Vagas Full', |
| 147 | + webPreferences: { |
| 148 | + preload: path.join(__dirname, 'preload.js'), |
| 149 | + contextIsolation: true, |
| 150 | + nodeIntegration: false, |
| 151 | + sandbox: true, |
| 152 | + }, |
| 153 | + }); |
| 154 | + |
| 155 | + // The React app is served by Express as static files. |
| 156 | + // loadURL keeps relative /api/... fetch calls working correctly. |
| 157 | + win.loadURL(BACKEND_URL); |
| 158 | + win.once('ready-to-show', () => win.show()); |
| 159 | + return win; |
| 160 | +} |
| 161 | + |
| 162 | +// ─── App lifecycle ───────────────────────────────────────────────────────────── |
| 163 | + |
| 164 | +app.whenReady().then(async () => { |
| 165 | + const loader = createLoadingWindow(); |
| 166 | + |
| 167 | + try { |
| 168 | + // 1. Start Express (backend + static file serving) inside this process |
| 169 | + await startBackend(); |
| 170 | + |
| 171 | + // 2. Wait until the HTTP server is accepting connections |
| 172 | + await waitForBackend(); |
| 173 | + |
| 174 | + // 3. Open the dashboard; destroy loading screen when page is ready |
| 175 | + const main = createMainWindow(); |
| 176 | + main.once('ready-to-show', () => loader.destroy()); |
| 177 | + |
| 178 | + } catch (fatal) { |
| 179 | + loader.destroy(); |
| 180 | + dialog.showErrorBox( |
| 181 | + 'Falha ao iniciar o aplicativo', |
| 182 | + `O servidor não pôde ser iniciado.\n\n${fatal.message}`, |
| 183 | + ); |
| 184 | + app.quit(); |
| 185 | + } |
| 186 | + |
| 187 | + // macOS: recreate window when dock icon is clicked |
| 188 | + app.on('activate', () => { |
| 189 | + if (BrowserWindow.getAllWindows().length === 0) createMainWindow(); |
| 190 | + }); |
| 191 | +}); |
| 192 | + |
| 193 | +app.on('window-all-closed', () => { |
| 194 | + if (process.platform !== 'darwin') app.quit(); |
| 195 | +}); |
0 commit comments