Skip to content
Open
Changes from 1 commit
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
34 changes: 27 additions & 7 deletions electron/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,29 @@ 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));
.on('error', () => {
if (Date.now() - start > timeout) {
reject(new Error('Server did not start within timeout'));
} else {
setTimeout(check, 500);
}
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
};

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 +71,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 +106,13 @@ function createWindow() {

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

// Cleanup
Expand Down
Loading