Treasury & Bank Transfer Management System — سامانه مدیریت خزانه‌داری و حواله‌های بانکی

1 const { app, BrowserWindow, shell } = require('electron');
2 const { spawn } = require('node:child_process');
3 const path = require('node:path');
4 const http = require('node:http');
5
6 let serverProcess;
7 const PORT = 3210;
8
9 function waitForServer(url, timeoutMs = 30000) {
10 const started = Date.now();
11 return new Promise((resolve, reject) => {
12 const attempt = () => {
13 const req = http.get(url, res => {
14 res.resume();
15 if (res.statusCode && res.statusCode < 500) return resolve();
16 retry();
17 });
18 req.on('error', retry);
19 req.setTimeout(1500, () => { req.destroy(); retry(); });
20 };
21 const retry = () => {
22 if (Date.now() - started > timeoutMs) return reject(new Error('TreasuryFlow local server did not start'));
23 setTimeout(attempt, 300);
24 };
25 attempt();
26 });
27 }
28
29 function serverPath() {
30 if (app.isPackaged) return path.join(process.resourcesPath, 'standalone', 'server.js');
31 return path.join(app.getAppPath(), '.next', 'standalone', 'server.js');
32 }
33
34 async function startServer() {
35 const script = serverPath();
36 serverProcess = spawn(process.execPath, [script], {
37 cwd: path.dirname(script),
38 windowsHide: true,
39 stdio: 'ignore',
40 env: {
41 ...process.env,
42 ELECTRON_RUN_AS_NODE: '1',
43 NODE_ENV: 'production',
44 HOSTNAME: '127.0.0.1',
45 PORT: String(PORT),
46 TREASURYFLOW_DESKTOP_LOCAL: '1',
47 BANK_ADAPTER_MODE: 'disabled'
48 }
49 });
50 serverProcess.on('exit', code => {
51 if (!app.isQuitting && code !== 0) console.error('TreasuryFlow server exited', code);
52 });
53 await waitForServer(`http://127.0.0.1:${PORT}/api/ready`);
54 }
55
56 async function createWindow() {
57 await startServer();
58 const win = new BrowserWindow({
59 width: 1440,
60 height: 920,
61 minWidth: 1024,
62 minHeight: 700,
63 show: false,
64 autoHideMenuBar: true,
65 webPreferences: { nodeIntegration: false, contextIsolation: true, sandbox: true }
66 });
67 win.webContents.setWindowOpenHandler(({ url }) => {
68 if (/^https?:\/\//i.test(url)) shell.openExternal(url);
69 return { action: 'deny' };
70 });
71 win.webContents.on('will-navigate', (event, url) => {
72 if (!url.startsWith(`http://127.0.0.1:${PORT}`)) event.preventDefault();
73 });
74 await win.loadURL(`http://127.0.0.1:${PORT}`);
75 win.once('ready-to-show', () => win.show());
76 }
77
78 app.whenReady().then(createWindow).catch(err => {
79 console.error(err);
80 app.quit();
81 });
82 app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit(); });
83 app.on('before-quit', () => { app.isQuitting = true; if (serverProcess) serverProcess.kill(); });