feat: add HTTP server and port argument support for electron
This commit is contained in:
@@ -2,11 +2,15 @@ import { app, BrowserWindow, ipcMain } from 'electron';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import fs from 'fs';
|
||||
import http from 'http';
|
||||
|
||||
app.commandLine.appendSwitch('disable-gpu-shader-disk-cache');
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
let mainWindow = null;
|
||||
let httpServer = null;
|
||||
const LOG_FILE = app.isPackaged
|
||||
? path.join(app.getPath('userData'), 'electron.log')
|
||||
: path.join(__dirname, '..', 'electron.log');
|
||||
@@ -20,7 +24,7 @@ function log(...args) {
|
||||
}
|
||||
|
||||
function parseArgs() {
|
||||
const args = { docs: null, headless: false };
|
||||
const args = { docs: null, headless: false, port: null };
|
||||
const docsIndex = process.argv.indexOf('--docs');
|
||||
if (docsIndex !== -1 && process.argv[docsIndex + 1]) {
|
||||
args.docs = process.argv[docsIndex + 1];
|
||||
@@ -28,6 +32,10 @@ function parseArgs() {
|
||||
if (process.argv.includes('--headless')) {
|
||||
args.headless = true;
|
||||
}
|
||||
const portIndex = process.argv.indexOf('--port');
|
||||
if (portIndex !== -1 && process.argv[portIndex + 1]) {
|
||||
args.port = parseInt(process.argv[portIndex + 1], 10);
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
@@ -40,6 +48,64 @@ function getIndexPath() {
|
||||
return path.join(__dirname, '..', 'dist', 'index.html');
|
||||
}
|
||||
|
||||
function startHttpServer(port) {
|
||||
const distPath = app.isPackaged
|
||||
? path.join(__dirname, '..', 'dist')
|
||||
: path.join(__dirname, '..', 'dist');
|
||||
|
||||
const mimeTypes = {
|
||||
'.html': 'text/html',
|
||||
'.js': 'application/javascript',
|
||||
'.css': 'text/css',
|
||||
'.json': 'application/json',
|
||||
'.png': 'image/png',
|
||||
'.jpg': 'image/jpeg',
|
||||
'.jpeg': 'image/jpeg',
|
||||
'.gif': 'image/gif',
|
||||
'.svg': 'image/svg+xml',
|
||||
'.ico': 'image/x-icon',
|
||||
'.woff': 'font/woff',
|
||||
'.woff2': 'font/woff2',
|
||||
'.ttf': 'font/ttf',
|
||||
'.eot': 'application/vnd.ms-fontobject',
|
||||
};
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
let filePath = path.join(distPath, req.url === '/' ? 'index.html' : req.url);
|
||||
|
||||
const ext = path.extname(filePath);
|
||||
const contentType = mimeTypes[ext] || 'application/octet-stream';
|
||||
|
||||
fs.readFile(filePath, (err, content) => {
|
||||
if (err) {
|
||||
if (err.code === 'ENOENT') {
|
||||
fs.readFile(path.join(distPath, 'index.html'), (err2, content2) => {
|
||||
if (err2) {
|
||||
res.writeHead(404);
|
||||
res.end('Not Found');
|
||||
} else {
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
res.end(content2);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
res.writeHead(500);
|
||||
res.end('Server Error');
|
||||
}
|
||||
} else {
|
||||
res.writeHead(200, { 'Content-Type': contentType });
|
||||
res.end(content);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(port, '0.0.0.0', () => {
|
||||
log(`HTTP server running at http://localhost:${port}/`);
|
||||
});
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
function createWindow() {
|
||||
const indexPath = getIndexPath();
|
||||
log('Creating window, indexPath:', indexPath);
|
||||
@@ -48,18 +114,28 @@ function createWindow() {
|
||||
log('index.html exists:', fs.existsSync(indexPath));
|
||||
|
||||
mainWindow = new BrowserWindow({
|
||||
show: !args.headless,
|
||||
show: false,
|
||||
width: 1200,
|
||||
height: 800,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, 'preload.cjs'),
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false,
|
||||
devTools: true,
|
||||
devTools: !app.isPackaged,
|
||||
},
|
||||
});
|
||||
|
||||
mainWindow.loadFile(indexPath);
|
||||
if (args.port) {
|
||||
mainWindow.loadURL(`http://localhost:${args.port}/`);
|
||||
} else {
|
||||
mainWindow.loadFile(indexPath);
|
||||
}
|
||||
|
||||
mainWindow.on('ready-to-show', () => {
|
||||
if (!args.headless) {
|
||||
mainWindow.show();
|
||||
}
|
||||
});
|
||||
|
||||
mainWindow.webContents.on('did-fail-load', (_event, errorCode, errorDesc) => {
|
||||
log('FAIL LOAD:', errorCode, errorDesc);
|
||||
@@ -69,7 +145,7 @@ function createWindow() {
|
||||
log('Page finished loading');
|
||||
});
|
||||
|
||||
if (!args.headless) {
|
||||
if (!app.isPackaged && !args.headless) {
|
||||
mainWindow.webContents.openDevTools();
|
||||
}
|
||||
|
||||
@@ -119,7 +195,13 @@ ipcMain.on('renderer-log', (_event, level, ...args) => {
|
||||
});
|
||||
|
||||
app.whenReady().then(() => {
|
||||
log('App ready, creating window');
|
||||
log('App ready, parsing args:', args);
|
||||
|
||||
if (args.port) {
|
||||
log('Starting HTTP server on port', args.port);
|
||||
httpServer = startHttpServer(args.port);
|
||||
}
|
||||
|
||||
createWindow();
|
||||
|
||||
app.on('activate', () => {
|
||||
@@ -133,6 +215,9 @@ app.whenReady().then(() => {
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (httpServer) {
|
||||
httpServer.close();
|
||||
}
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user