Allow use of env variables

Made some changes that allows the use of environment variables when starting the standalone client.
This commit is contained in:
Unknown 2017-06-29 21:32:48 +02:00 committed by unknown
parent 1590693547
commit 8eb772d80b

View File

@ -5,31 +5,22 @@
// Use seperate scope to prevent global scope pollution // Use seperate scope to prevent global scope pollution
(function () { (function () {
const cookie = require("cookie"); const cookie = require("cookie");
var config = {}; var config = {};
// Parse command line arguments, if any // Helper function to get server address/hostname from either the commandline or env
var addressIndex = process.argv.indexOf("--address"); function getServerAddress() {
var portIndex = process.argv.indexOf("--port"); // Helper function to get command line parameters
// Assumes that a cmdline parameter is defined with `--key [value]`
if (addressIndex > -1) { function getCommandLineParameter(key, defaultValue = undefined) {
config.address = process.argv[addressIndex + 1]; var index = process.argv.indexOf(`--${key}`);
} else { var value = index > -1 ? process.argv[index + 1] : undefined;
fail(); return value !== undefined ? String(value) : defaultValue;
}
if (portIndex > -1) {
config.port = process.argv[portIndex + 1];
} else {
fail();
}
function fail(message, code = 1) {
if (message !== undefined && typeof message === "string") {
console.log(message);
} else {
console.log("Usage: 'node clientonly --address 192.168.1.10 --port 8080'");
} }
process.exit(code);
// Prefer command line arguments over environment variables
["address", "port"].forEach((key) => {
config[key] = getCommandLineParameter(key, process.env[key.toUpperCase()]);
})
} }
function getServerConfig(url) { function getServerConfig(url) {
@ -56,6 +47,19 @@
}) })
}; };
function fail(message, code = 1) {
if (message !== undefined && typeof message === "string") {
console.log(message);
} else {
console.log("Usage: 'node clientonly --address 192.168.1.10 --port 8080'");
}
process.exit(code);
}
getServerAddress();
(config.address && config.port) || fail();
// Only start the client if a non-local server was provided // Only start the client if a non-local server was provided
if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].indexOf(config.address) === -1) { if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].indexOf(config.address) === -1) {
getServerConfig(`http://${config.address}:${config.port}/config/`) getServerConfig(`http://${config.address}:${config.port}/config/`)