From 7eafa305e529ddc08d3c656e223529a3129cdf65 Mon Sep 17 00:00:00 2001 From: RblSb Date: Mon, 9 Jan 2023 14:15:48 +0300 Subject: Add --port argument for fast multichannel setups --- src/server/Main.hx | 8 +++++++- src/server/Utils.hx | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/server/Main.hx b/src/server/Main.hx index 282c09a..a525f65 100644 --- a/src/server/Main.hx +++ b/src/server/Main.hx @@ -63,7 +63,8 @@ class Main { function new(opts:MainOptions) { isNoState = !opts.loadState; - verbose = Sys.args().has("--verbose"); + final args = Utils.parseArgs(Sys.args(), false); + verbose = args.exists("verbose"); statePath = '$rootDir/user/state.json'; logsDir = '$rootDir/user/logs'; // process.on("exit", exit); @@ -97,6 +98,11 @@ class Main { port = config.port; final envPort = (process.env : Dynamic).PORT; if (envPort != null) port = envPort; + final argPort = args["port"]; + if (argPort != null) { + final newPort = Std.parseInt(argPort); + if (newPort != null) port = newPort; + } var attempts = isNoState ? 500 : 5; function preparePort():Void { diff --git a/src/server/Utils.hx b/src/server/Utils.hx index a2f4382..43160e6 100644 --- a/src/server/Utils.hx +++ b/src/server/Utils.hx @@ -6,7 +6,30 @@ import js.node.Os; import js.node.url.URL; import sys.FileSystem; +using StringTools; + class Utils { + public static function parseArgs(args:Array, caseSensitive = true):Map { + final map:Map = []; + for (arg in args) { + if (arg.length == 0) continue; + if (arg.fastCodeAt(0) == "-".code) { + arg = arg.substr(arg.fastCodeAt(1) == "-".code ? 2 : 1); + } + final delimiterPos = arg.indexOf("="); + if (delimiterPos < 1) { + final key = caseSensitive ? arg : arg.toLowerCase(); + map[key] = ""; + continue; + } + var key = arg.substr(0, delimiterPos); + if (!caseSensitive) key = key.toLowerCase(); + map[key] = arg.substr(delimiterPos + 1); + } + return map; + } + public static function ensureDir(path:String):Void { if (!FileSystem.exists(path)) FileSystem.createDirectory(path); } -- cgit v1.2.3