feat(server): enhance server management with process forking and implement server origin retrieval

This commit is contained in:
Anish Sarkar 2026-05-25 17:52:10 +05:30
parent 7be4231ad4
commit a2847664c8
2 changed files with 56 additions and 7 deletions

View file

@ -1,9 +1,10 @@
import path from 'path'; import path from 'path';
import { app } from 'electron'; import { app, utilityProcess } from 'electron';
import { getPort } from 'get-port-please'; import { getPort } from 'get-port-please';
const isDev = !app.isPackaged; const isDev = !app.isPackaged;
let serverPort = 3000; let serverPort = 3000;
let nextServerProcess: ReturnType<typeof utilityProcess.fork> | null = null;
export function getServerPort(): number { export function getServerPort(): number {
return serverPort; return serverPort;
@ -38,16 +39,54 @@ export async function startNextServer(): Promise<void> {
const standalonePath = getStandalonePath(); const standalonePath = getStandalonePath();
const serverScript = path.join(standalonePath, 'server.js'); const serverScript = path.join(standalonePath, 'server.js');
process.env.PORT = String(serverPort); const child = utilityProcess.fork(serverScript, [], {
process.env.HOSTNAME = '0.0.0.0'; cwd: standalonePath,
process.env.NODE_ENV = 'production'; env: {
process.chdir(standalonePath); ...process.env,
PORT: String(serverPort),
HOSTNAME: '127.0.0.1',
NODE_ENV: 'production',
},
serviceName: 'SurfSense Next Server',
stdio: 'pipe',
});
nextServerProcess = child;
require(serverScript); child.stdout?.on('data', (chunk) => {
process.stdout.write(chunk);
});
child.stderr?.on('data', (chunk) => {
process.stderr.write(chunk);
});
const ready = await waitForServer(`http://localhost:${serverPort}`); const handleExit = (code: number) => {
if (nextServerProcess === child) {
nextServerProcess = null;
}
console.error(`Next.js server exited with code ${code}`);
};
child.on('exit', handleExit);
let startupExitHandler: ((code: number) => void) | null = null;
const exited = new Promise<never>((_resolve, reject) => {
startupExitHandler = (code: number) => {
reject(new Error(`Next.js server exited before startup completed with code ${code}`));
};
child.once('exit', startupExitHandler);
});
const ready = await Promise.race([waitForServer(`http://localhost:${serverPort}`), exited]);
if (startupExitHandler) {
child.removeListener('exit', startupExitHandler);
}
if (!ready) { if (!ready) {
stopNextServer();
throw new Error('Next.js server failed to start within 30 s'); throw new Error('Next.js server failed to start within 30 s');
} }
console.log(`Next.js server ready on port ${serverPort}`); console.log(`Next.js server ready on port ${serverPort}`);
} }
export function stopNextServer(): void {
nextServerProcess?.kill();
nextServerProcess = null;
}

View file

@ -8,6 +8,7 @@ import { setActiveSearchSpaceId } from './active-search-space';
const isDev = !app.isPackaged; const isDev = !app.isPackaged;
const HOSTED_FRONTEND_URL = process.env.HOSTED_FRONTEND_URL as string; const HOSTED_FRONTEND_URL = process.env.HOSTED_FRONTEND_URL as string;
const isMac = process.platform === 'darwin'; const isMac = process.platform === 'darwin';
const WINDOW_TITLE = 'SurfSense';
let mainWindow: BrowserWindow | null = null; let mainWindow: BrowserWindow | null = null;
let isQuitting = false; let isQuitting = false;
@ -24,6 +25,7 @@ export function markQuitting(): void {
export function createMainWindow(initialPath = '/dashboard'): BrowserWindow { export function createMainWindow(initialPath = '/dashboard'): BrowserWindow {
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
title: WINDOW_TITLE,
width: 1280, width: 1280,
height: 800, height: 800,
minWidth: 800, minWidth: 800,
@ -48,6 +50,14 @@ export function createMainWindow(initialPath = '/dashboard'): BrowserWindow {
mainWindow?.show(); mainWindow?.show();
}); });
mainWindow.webContents.on('page-title-updated', (event) => {
event.preventDefault();
mainWindow?.setTitle(WINDOW_TITLE);
});
mainWindow.webContents.on('did-finish-load', () => {
mainWindow?.setTitle(WINDOW_TITLE);
});
mainWindow.loadURL(`http://localhost:${getServerPort()}${initialPath}`); mainWindow.loadURL(`http://localhost:${getServerPort()}${initialPath}`);
mainWindow.webContents.setWindowOpenHandler(({ url }) => { mainWindow.webContents.setWindowOpenHandler(({ url }) => {