Remove backend from electon, switching to server manager
This commit is contained in:
@@ -1,68 +1,26 @@
|
|||||||
const { app, BrowserWindow, Menu, shell, ipcMain } = require('electron');
|
const { app, BrowserWindow, shell, ipcMain } = require('electron');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
|
||||||
|
|
||||||
// Global variables
|
|
||||||
let mainWindow = null;
|
let mainWindow = null;
|
||||||
let backendPort = null;
|
|
||||||
|
|
||||||
// Development environment detection
|
|
||||||
const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;
|
const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;
|
||||||
|
|
||||||
const BACKEND_CONFIG = {
|
|
||||||
port: 18080
|
|
||||||
};
|
|
||||||
|
|
||||||
// Prevent multiple instances
|
|
||||||
const gotTheLock = app.requestSingleInstanceLock();
|
const gotTheLock = app.requestSingleInstanceLock();
|
||||||
if (!gotTheLock) {
|
if (!gotTheLock) {
|
||||||
|
console.log('Another instance is already running, quitting...');
|
||||||
app.quit();
|
app.quit();
|
||||||
|
process.exit(0);
|
||||||
} else {
|
} else {
|
||||||
app.on('second-instance', () => {
|
app.on('second-instance', (event, commandLine, workingDirectory) => {
|
||||||
|
console.log('Second instance detected, focusing existing window...');
|
||||||
if (mainWindow) {
|
if (mainWindow) {
|
||||||
if (mainWindow.isMinimized()) mainWindow.restore();
|
if (mainWindow.isMinimized()) mainWindow.restore();
|
||||||
mainWindow.focus();
|
mainWindow.focus();
|
||||||
|
mainWindow.show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple backend management
|
|
||||||
async function startBackend() {
|
|
||||||
try {
|
|
||||||
console.log('Starting backend...');
|
|
||||||
|
|
||||||
// Set up environment
|
|
||||||
process.env.NODE_ENV = 'production';
|
|
||||||
process.env.PORT = BACKEND_CONFIG.port.toString();
|
|
||||||
process.env.DATA_PATH = app.getPath('userData');
|
|
||||||
process.env.DB_PATH = path.join(app.getPath('userData'), 'database.db');
|
|
||||||
process.env.VERSION = app.getVersion();
|
|
||||||
|
|
||||||
// Get backend path
|
|
||||||
const backendPath = isDev
|
|
||||||
? path.join(__dirname, '..', 'dist', 'backend', 'starter.js')
|
|
||||||
: path.join(process.resourcesPath, 'dist', 'backend', 'starter.js');
|
|
||||||
|
|
||||||
console.log('Loading backend from:', backendPath);
|
|
||||||
|
|
||||||
if (!fs.existsSync(backendPath)) {
|
|
||||||
console.error('Backend file not found at:', backendPath);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load and start the backend
|
|
||||||
const { startServer } = await import(backendPath);
|
|
||||||
backendPort = await startServer();
|
|
||||||
console.log('Backend started successfully on port:', backendPort);
|
|
||||||
return backendPort;
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to start backend:', error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create main window
|
|
||||||
function createWindow() {
|
function createWindow() {
|
||||||
mainWindow = new BrowserWindow({
|
mainWindow = new BrowserWindow({
|
||||||
width: 1200,
|
width: 1200,
|
||||||
@@ -81,29 +39,24 @@ function createWindow() {
|
|||||||
show: false
|
show: false
|
||||||
});
|
});
|
||||||
|
|
||||||
// Remove menu bar on Windows/Linux
|
|
||||||
if (process.platform !== 'darwin') {
|
if (process.platform !== 'darwin') {
|
||||||
mainWindow.setMenuBarVisibility(false);
|
mainWindow.setMenuBarVisibility(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load application
|
|
||||||
if (isDev) {
|
if (isDev) {
|
||||||
mainWindow.loadURL('http://localhost:5173');
|
mainWindow.loadURL('http://localhost:5173');
|
||||||
mainWindow.webContents.openDevTools();
|
mainWindow.webContents.openDevTools();
|
||||||
} else {
|
} else {
|
||||||
// Load from dist folder
|
|
||||||
const indexPath = path.join(__dirname, '..', 'dist', 'index.html');
|
const indexPath = path.join(__dirname, '..', 'dist', 'index.html');
|
||||||
console.log('Loading frontend from:', indexPath);
|
console.log('Loading frontend from:', indexPath);
|
||||||
mainWindow.loadFile(indexPath);
|
mainWindow.loadFile(indexPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show window when ready
|
|
||||||
mainWindow.once('ready-to-show', () => {
|
mainWindow.once('ready-to-show', () => {
|
||||||
console.log('Window ready to show');
|
console.log('Window ready to show');
|
||||||
mainWindow.show();
|
mainWindow.show();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle load errors
|
|
||||||
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL) => {
|
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL) => {
|
||||||
console.error('Failed to load:', errorCode, errorDescription, validatedURL);
|
console.error('Failed to load:', errorCode, errorDescription, validatedURL);
|
||||||
});
|
});
|
||||||
@@ -112,7 +65,6 @@ function createWindow() {
|
|||||||
console.log('Frontend loaded successfully');
|
console.log('Frontend loaded successfully');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle window close
|
|
||||||
mainWindow.on('close', (event) => {
|
mainWindow.on('close', (event) => {
|
||||||
if (process.platform === 'darwin') {
|
if (process.platform === 'darwin') {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@@ -124,18 +76,12 @@ function createWindow() {
|
|||||||
mainWindow = null;
|
mainWindow = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle external links
|
|
||||||
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
||||||
shell.openExternal(url);
|
shell.openExternal(url);
|
||||||
return { action: 'deny' };
|
return { action: 'deny' };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPC handlers
|
|
||||||
ipcMain.handle('get-backend-port', () => {
|
|
||||||
return backendPort;
|
|
||||||
});
|
|
||||||
|
|
||||||
ipcMain.handle('get-app-version', () => {
|
ipcMain.handle('get-app-version', () => {
|
||||||
return app.getVersion();
|
return app.getVersion();
|
||||||
});
|
});
|
||||||
@@ -144,30 +90,8 @@ ipcMain.handle('get-platform', () => {
|
|||||||
return process.platform;
|
return process.platform;
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcMain.handle('restart-backend', async () => {
|
app.whenReady().then(() => {
|
||||||
try {
|
|
||||||
backendPort = await startBackend();
|
|
||||||
return { success: true, port: backendPort };
|
|
||||||
} catch (error) {
|
|
||||||
return { success: false, error: error.message };
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// App event handlers
|
|
||||||
app.whenReady().then(async () => {
|
|
||||||
// Create window immediately for fast startup
|
|
||||||
createWindow();
|
createWindow();
|
||||||
|
|
||||||
// Start backend in background (non-blocking)
|
|
||||||
try {
|
|
||||||
backendPort = await startBackend();
|
|
||||||
if (backendPort) {
|
|
||||||
console.log('Backend started successfully on port:', backendPort);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Backend failed to start:', error);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Termix started successfully');
|
console.log('Termix started successfully');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -189,7 +113,10 @@ app.on('before-quit', () => {
|
|||||||
console.log('App is quitting...');
|
console.log('App is quitting...');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Error handling
|
app.on('will-quit', () => {
|
||||||
|
console.log('App will quit...');
|
||||||
|
});
|
||||||
|
|
||||||
process.on('uncaughtException', (error) => {
|
process.on('uncaughtException', (error) => {
|
||||||
console.error('Uncaught Exception:', error);
|
console.error('Uncaught Exception:', error);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user