-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathmain.cjs
More file actions
130 lines (113 loc) · 3.02 KB
/
Copy pathmain.cjs
File metadata and controls
130 lines (113 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const { app, BrowserWindow } = require('electron');
const path = require('path');
const { spawn } = require('child_process');
const http = require('http');
const fs = require('fs');
let mainWindow;
let serverProcess;
let serverHost = '0.0.0.0';
let serverPort = 3000;
try {
const configPath = './src/server-config.json';
if (fs.existsSync(configPath)) {
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
if (config.host) serverHost = config.host;
if (config.frontendPort) serverPort = config.frontendPort;
}
} catch (e) {
console.warn('Failed to load server config:', e);
}
// Prevent multiple instances
const gotLock = app.requestSingleInstanceLock();
if (!gotLock) {
app.quit();
process.exit(0);
}
// Wait until server is ready
function waitForServer(url, maxRetries = 60) {
return new Promise((resolve, reject) => {
let attempts = 0;
const check = () => {
attempts++;
http
.get(url, () => resolve())
.on('error', () => {
if (attempts >= maxRetries) {
reject(new Error(`Server failed to start after ${maxRetries} attempts (${maxRetries * 0.5}s)`));
} else {
setTimeout(check, 500);
}
});
};
check();
});
}
// Start Nitro server (production)
function startServer() {
return new Promise((resolve, reject) => {
const serverPath = path.join(
process.resourcesPath,
'app.asar.unpacked',
'.output',
'server',
'index.mjs'
);
console.log("Starting server from:", serverPath);
serverProcess = spawn('node', [serverPath], {
stdio: 'ignore', // no terminal
windowsHide: true, // hide CMD
env: {
...process.env,
HOST: serverHost,
PORT: serverPort.toString(),
},
});
waitForServer(`http://localhost:${serverPort}`)
.then(resolve)
.catch(reject);
});
}
// Create window
function createWindow() {
if (mainWindow) return;
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
show: false,
});
mainWindow.loadURL(`http://localhost:${serverPort}`);
// Show when ready
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
// Debug only if needed
mainWindow.webContents.on('did-fail-load', (e, code, desc) => {
console.log("LOAD FAILED:", code, desc);
});
}
// App start
app.whenReady().then(async () => {
try {
await startServer();
createWindow();
} catch (error) {
console.error('Failed to start server:', error.message);
app.quit();
}
});
// Cleanup
app.on('window-all-closed', () => {
if (serverProcess) serverProcess.kill();
if (process.platform !== 'darwin') app.quit();
});
// Handle process termination signals for clean shutdown
process.on('SIGTERM', () => {
console.log('Received SIGTERM, shutting down gracefully');
if (serverProcess) serverProcess.kill();
app.quit();
});
process.on('SIGINT', () => {
console.log('Received SIGINT, shutting down gracefully');
if (serverProcess) serverProcess.kill();
app.quit();
});