From dcebcb0e73aa6d89816a28c81922e86d6b560a99 Mon Sep 17 00:00:00 2001 From: Xavier NUNN Date: Tue, 9 Jul 2019 22:50:20 +0200 Subject: [PATCH] Add host option (#588) * Added option to configure host * Updated sample config --- config-sample.ini | 2 ++ src/services/host.js | 10 ++++++++++ src/www | 7 +++++-- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 src/services/host.js diff --git a/config-sample.ini b/config-sample.ini index bc88f6e1a..3292540d8 100644 --- a/config-sample.ini +++ b/config-sample.ini @@ -3,6 +3,8 @@ instanceName= [Network] +# host setting is relevant only for web deployments - set the host on which the server will listen +# host=0.0.0.0 # port setting is relevant only for web deployments, desktop builds run on random free port port=8080 # true for TLS/SSL/HTTPS (secure), false for HTTP (unsecure). diff --git a/src/services/host.js b/src/services/host.js new file mode 100644 index 000000000..e638931bd --- /dev/null +++ b/src/services/host.js @@ -0,0 +1,10 @@ +const config = require('./config'); +const env = require('./env'); + +let environmentHost; + +if (process.env.TRILIUM_HOST) { + environmentHost = process.env.TRILIUM_HOST; +} + +module.exports = Promise.resolve(environmentHost || config['Network']['host'] || '0.0.0.0'); diff --git a/src/www b/src/www index d15d679b2..d775fce35 100755 --- a/src/www +++ b/src/www @@ -25,6 +25,7 @@ const messagingService = require('./services/messaging'); const utils = require('./services/utils'); const sqlInit = require('./services/sql_init'); const port = require('./services/port'); +const host = require('./services/host'); const semver = require('semver'); if (!semver.satisfies(process.version, ">=10.5.0")) { @@ -36,8 +37,10 @@ let httpServer; async function startTrilium() { const usedPort = await port; + const usedHost = await host; app.set('port', usedPort); + app.set('host', usedHost); if (config['Network']['https']) { if (!config['Network']['keyPath'] || !config['Network']['keyPath'].trim().length) { @@ -70,7 +73,7 @@ async function startTrilium() { */ httpServer.keepAliveTimeout = 120000 * 5; - httpServer.listen(usedPort); + httpServer.listen(usedPort, usedHost); httpServer.on('error', onError); httpServer.on('listening', () => debug('Listening on port' + httpServer.address().port)); @@ -108,4 +111,4 @@ function onError(error) { default: throw error; } -} \ No newline at end of file +}