feat: enhance port management and server initialization process

- Added a new function to check if a port is in use without terminating processes, improving user experience during server startup.
- Updated the health check function to accept a dynamic port parameter, allowing for flexible server configurations.
- Implemented user prompts for handling port conflicts, enabling users to kill processes, choose different ports, or cancel the operation.
- Enhanced CORS configuration to support localhost and IPv6 addresses, ensuring compatibility across different development environments.
- Refactored the main function to utilize dynamic port assignments for both the web and server applications, improving overall flexibility.
This commit is contained in:
WebDevCody
2026-01-01 00:42:42 -05:00
parent abe272ef4d
commit f32f3e82b2
3 changed files with 173 additions and 16 deletions

View File

@@ -53,9 +53,12 @@ let mainWindow: BrowserWindow | null = null;
let serverProcess: ChildProcess | null = null;
let staticServer: Server | null = null;
// Default ports - will be dynamically assigned if these are in use
const DEFAULT_SERVER_PORT = 3008;
const DEFAULT_STATIC_PORT = 3007;
// Default ports (can be overridden via env) - will be dynamically assigned if these are in use
// When launched via root init.mjs we pass:
// - PORT (backend)
// - TEST_PORT (vite dev server / static)
const DEFAULT_SERVER_PORT = parseInt(process.env.PORT || '3008', 10);
const DEFAULT_STATIC_PORT = parseInt(process.env.TEST_PORT || '3007', 10);
// Actual ports in use (set during startup)
let serverPort = DEFAULT_SERVER_PORT;
@@ -75,7 +78,9 @@ function isPortAvailable(port: number): Promise<boolean> {
resolve(true);
});
});
server.listen(port, '127.0.0.1');
// Use Node's default binding semantics (matches most dev servers)
// This avoids false-positives when a port is taken on IPv6/dual-stack.
server.listen(port);
});
}