electron build uses random free port, fixes #142

This commit is contained in:
azivner 2018-07-31 19:50:18 +02:00
parent 365c37604b
commit 9452fc236b
5 changed files with 51 additions and 75 deletions

View File

@ -3,6 +3,7 @@
instanceName= instanceName=
[Network] [Network]
# port setting is relevant only for web deployments, desktop builds run on random free port
port=8080 port=8080
# true for TLS/SSL/HTTPS (secure), false for HTTP (unsecure). # true for TLS/SSL/HTTPS (secure), false for HTTP (unsecure).
https=false https=false

View File

@ -2,9 +2,9 @@
const electron = require('electron'); const electron = require('electron');
const path = require('path'); const path = require('path');
const config = require('./src/services/config');
const log = require('./src/services/log'); const log = require('./src/services/log');
const url = require("url"); const url = require("url");
const port = require('./src/services/port');
const app = electron.app; const app = electron.app;
const globalShortcut = electron.globalShortcut; const globalShortcut = electron.globalShortcut;
@ -23,7 +23,7 @@ function onClosed() {
mainWindow = null; mainWindow = null;
} }
function createMainWindow() { async function createMainWindow() {
const win = new electron.BrowserWindow({ const win = new electron.BrowserWindow({
width: 1200, width: 1200,
height: 900, height: 900,
@ -31,10 +31,8 @@ function createMainWindow() {
icon: path.join(__dirname, 'src/public/images/app-icons/png/256x256.png') icon: path.join(__dirname, 'src/public/images/app-icons/png/256x256.png')
}); });
const port = config['Network']['port'] || '3000';
win.setMenu(null); win.setMenu(null);
win.loadURL('http://localhost:' + port); win.loadURL('http://localhost:' + await port);
win.on('closed', onClosed); win.on('closed', onClosed);
win.webContents.on('new-window', (e, url) => { win.webContents.on('new-window', (e, url) => {

View File

@ -37,6 +37,7 @@
"express": "~4.16.3", "express": "~4.16.3",
"express-session": "^1.15.6", "express-session": "^1.15.6",
"fs-extra": "^6.0.1", "fs-extra": "^6.0.1",
"get-port": "^4.0.0",
"helmet": "^3.12.1", "helmet": "^3.12.1",
"html": "^1.0.0", "html": "^1.0.0",
"image-type": "^3.0.0", "image-type": "^3.0.0",

10
src/services/port.js Normal file
View File

@ -0,0 +1,10 @@
const getPort = require('get-port');
const config = require('./config');
const utils = require('./utils');
if (utils.isElectron()) {
module.exports = getPort();
}
else {
module.exports = Promise.resolve(config['Network']['port'] || '3000');
}

62
src/www
View File

@ -18,16 +18,16 @@ const log = require('./services/log');
const appInfo = require('./services/app_info'); const appInfo = require('./services/app_info');
const messagingService = require('./services/messaging'); const messagingService = require('./services/messaging');
const utils = require('./services/utils'); const utils = require('./services/utils');
const sqlInit = require('./services/sql_init.js'); const sqlInit = require('./services/sql_init');
const port = require('./services/port');
const port = normalizePort(config['Network']['port'] || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
let httpServer; let httpServer;
async function startTrilium() {
const usedPort = await port;
app.set('port', usedPort);
if (config['Network']['https']) { if (config['Network']['https']) {
const options = { const options = {
key: fs.readFileSync(config['Network']['keyPath']), key: fs.readFileSync(config['Network']['keyPath']),
@ -36,12 +36,12 @@ if (config['Network']['https']) {
httpServer = https.createServer(options, app); httpServer = https.createServer(options, app);
log.info("App HTTPS server starting up at port " + port); log.info("App HTTPS server starting up at port " + usedPort);
} }
else { else {
httpServer = http.createServer(app); httpServer = http.createServer(app);
log.info("App HTTP server starting up at port " + port); log.info("App HTTP server starting up at port " + usedPort);
} }
log.info(JSON.stringify(appInfo, null, 2)); log.info(JSON.stringify(appInfo, null, 2));
@ -51,9 +51,9 @@ log.info(JSON.stringify(appInfo, null, 2));
*/ */
httpServer.keepAliveTimeout = 120000 * 5; httpServer.keepAliveTimeout = 120000 * 5;
httpServer.listen(port); httpServer.listen(usedPort);
httpServer.on('error', onError); httpServer.on('error', onError);
httpServer.on('listening', onListening); httpServer.on('listening', () => debug('Listening on port' + httpServer.address().port));
sqlInit.dbReady.then(() => messagingService.init(httpServer, sessionParser)); sqlInit.dbReady.then(() => messagingService.init(httpServer, sessionParser));
@ -61,26 +61,9 @@ if (utils.isElectron()) {
const electronRouting = require('./routes/electron'); const electronRouting = require('./routes/electron');
electronRouting(app); electronRouting(app);
} }
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
const port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
} }
if (port >= 0) { startTrilium();
// port number
return port;
}
return false;
}
/** /**
* Event listener for HTTP server "error" event. * Event listener for HTTP server "error" event.
@ -91,19 +74,15 @@ function onError(error) {
throw error; throw error;
} }
const bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages // handle specific listen errors with friendly messages
switch (error.code) { switch (error.code) {
case 'EACCES': case 'EACCES':
console.error(bind + ' requires elevated privileges'); console.error('Port requires elevated privileges');
process.exit(1); process.exit(1);
break; break;
case 'EADDRINUSE': case 'EADDRINUSE':
console.error(bind + ' is already in use'); console.error('Port is already in use');
process.exit(1); process.exit(1);
break; break;
@ -111,16 +90,3 @@ function onError(error) {
throw error; throw error;
} }
} }
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
const addr = httpServer.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}