Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions electron/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,36 @@ if (!gotLock) {
}

// Wait until server is ready
function waitForServer(url) {
return new Promise((resolve) => {
function waitForServer(url, timeout = 15000) {
return new Promise((resolve, reject) => {
const start = Date.now();

const check = () => {
http
.get(url, () => resolve())
.on('error', () => setTimeout(check, 500));
const req = http.get(url, (res) => {
res.resume();
resolve();
});

req.setTimeout(2000, () => {
req.destroy();
});

req.on('error', () => {
if (Date.now() - start > timeout) {
reject(new Error('Server did not start within timeout'));
} else {
setTimeout(check, 500);
}
});
};

check();
});
}

// Start Nitro server (production)
function startServer() {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const serverPath = path.join(
process.resourcesPath,
'app.asar.unpacked',
Expand All @@ -62,7 +78,13 @@ function startServer() {
},
});

waitForServer(`http://localhost:${serverPort}`).then(resolve);
waitForServer(`http://localhost:${serverPort}`)
.then(resolve)
.catch((err) => {
console.error('Server failed to start:', err);
if (serverProcess) serverProcess.kill();
reject(err)
});
});
}

Expand Down Expand Up @@ -91,8 +113,14 @@ function createWindow() {

// App start
app.whenReady().then(async () => {
await startServer();
createWindow();
try {
await startServer();
createWindow();
} catch (e) {
console.error('Startup failed:', e);
if (serverProcess) serverProcess.kill();
app.quit();
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});

// Cleanup
Expand Down
Loading