From 0d3edd0a93e40024aafc69d7519a735d2a437cf6 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 23 May 2019 22:26:31 +0100
Subject: [PATCH] Get dependency injection (basically) working. Hooray!

---
 server/Helpers/Ansi.mjs        |  4 +---
 server/bootstrap/container.mjs |  7 ++++++-
 server/bootstrap/settings.mjs  | 15 ++++++---------
 server/help.mjs                | 29 ++++++++++++++++-------------
 server/index.mjs               | 11 ++++++-----
 settings.toml                  |  2 ++
 6 files changed, 37 insertions(+), 31 deletions(-)
 create mode 100644 settings.toml

diff --git a/server/Helpers/Ansi.mjs b/server/Helpers/Ansi.mjs
index 2c7eb5d..7fb8d79 100644
--- a/server/Helpers/Ansi.mjs
+++ b/server/Helpers/Ansi.mjs
@@ -75,6 +75,4 @@ class Ansi {
 	}
 }
 
-const ansi = new Ansi();
-
-export default ansi;
+export default Ansi;
diff --git a/server/bootstrap/container.mjs b/server/bootstrap/container.mjs
index 7ba9633..fa8dc32 100644
--- a/server/bootstrap/container.mjs
+++ b/server/bootstrap/container.mjs
@@ -2,14 +2,19 @@
 
 import a from 'awilix';
 
-import database_init from '../bootstrap/database_init.mjs';
+import Ansi from '../Helpers/Ansi.mjs';
 import TTNAppServer from '../ttn-app-server/TTNAppServer.mjs';
 
+import settings from './settings.mjs';
+import database_init from '../bootstrap/database_init.mjs';
+
 const c = a.createContainer({
 	injectionMode: a.InjectionMode.PROXY
 });
 
 c.register({
+	settings: a.asValue(settings),
+	ansi: a.asFunction(() => new Ansi()).singleton(),
 	database: a.asFunction(database_init).singleton(),
 	TTNAppServer: a.asClass(TTNAppServer),
 });
diff --git a/server/bootstrap/settings.mjs b/server/bootstrap/settings.mjs
index 1944bc2..814cf1c 100644
--- a/server/bootstrap/settings.mjs
+++ b/server/bootstrap/settings.mjs
@@ -1,26 +1,23 @@
 "use strict";
 
+import path from 'path';
 import fs from 'fs';
 
 import toml from '@iarna/toml';
 
-import c from './container.mjs';
-import apply_settings from './Helpers/apply_settings.mjs';
+import apply_settings from '../Helpers/apply_settings.mjs';
 
+const root_dir = path.dirname(process.argv[1]);
 
-let filename_default = "../settings.default.toml",
-	filename_custom = "../../settings.toml";
+let filename_default = path.resolve(root_dir, "./settings.default.toml"),
+	filename_custom = path.resolve(root_dir, "../settings.toml");
 
 if(!fs.existsSync(filename_custom))
 	fs.writeFileSync(filename_custom, `# Custom settings file. This file overrides server/settings.default.toml - refer there of example of settings you can override.\n\n`);
 
 let settings = toml.parse(fs.readFileSync(filename_default, "utf-8")),
-	settings_override = toml.parse(fs.readFileSync("../../settings.custom.toml", "utf-8"));
+	settings_override = toml.parse(fs.readFileSync(filename_custom, "utf-8"));
 
 apply_settings(settings, settings_override);
 
-c.register({
-	settings: a.asValue(settings)
-});
-
 export default settings;
diff --git a/server/help.mjs b/server/help.mjs
index 1fd6757..f52ea7f 100644
--- a/server/help.mjs
+++ b/server/help.mjs
@@ -1,27 +1,30 @@
 import path from 'path';
 
-import ansi from './Helpers/Ansi.mjs';
+import c from './bootstrap/container.mjs';
+
+const a = c.resolve("ansi");
 
 export default function(settings) {
+	
 	console.log(`${settings.program_name}, ${settings.version}
-${ansi.locol}By Starbeamrainbowlabs${ansi.reset}
+${a.locol}By Starbeamrainbowlabs${a.reset}
 
-${ansi.hicol}This program ${settings.description}.${ansi.reset}
+${a.hicol}This program ${settings.description}.${a.reset}
 
-${ansi.fblue}${ansi.hicol}Usage:${ansi.reset}
+${a.fblue}${a.hicol}Usage:${a.reset}
     node --experimental-modules ${path.relative(process.cwd(), process.argv[1])} {subcommand} {options}
 
-${ansi.fblue}${ansi.hicol}Subcommands:${ansi.reset}
-    ${ansi.fyellow}ttn-app-server${ansi.reset}    Starts the thing network application server
-    ${ansi.fyellow}process-data${ansi.reset}      Consolidates collected data from the IoT device and the TTN app server
-    ${ansi.fyellow}train-ai${ansi.reset}          Trains the AI on the consolidated data
-    ${ansi.fyellow}serve-map${ansi.reset}         Serves the final output map using the trained AI
+${a.fblue}${a.hicol}Subcommands:${a.reset}
+    ${a.fyellow}ttn-app-server${a.reset}    Starts the thing network application server
+    ${a.fyellow}process-data${a.reset}      Consolidates collected data from the IoT device and the TTN app server
+    ${a.fyellow}train-ai${a.reset}          Trains the AI on the consolidated data
+    ${a.fyellow}serve-map${a.reset}         Serves the final output map using the trained AI
 
-${ansi.fblue}${ansi.hicol}Options:${ansi.reset}
-    ${ansi.fyellow}-h	--help		${ansi.reset}Show this message
-    ${ansi.fyellow}-v	--version	${ansi.reset}Show the version of this program
+${a.fblue}${a.hicol}Options:${a.reset}
+    ${a.fyellow}-h	--help		${a.reset}Show this message
+    ${a.fyellow}-v	--version	${a.reset}Show the version of this program
 
-${ansi.fblue}${ansi.hicol}Environment Variables:${ansi.reset}
+${a.fblue}${a.hicol}Environment Variables:${a.reset}
     (none yet)
 `);
 };
diff --git a/server/index.mjs b/server/index.mjs
index c51c25c..dba8ae7 100755
--- a/server/index.mjs
+++ b/server/index.mjs
@@ -2,12 +2,13 @@
 "use strict";
 
 // 1: Setup & Imports
-import ansi from './Helpers/Ansi.mjs';
 import c from './bootstrap/container.mjs';
 
 import show_help from './help.mjs';
 import TTNAppServer from './ttn-app-server/TTNAppServer.mjs';
-import settings from './bootstrap/settings.mjs';
+
+const settings = c.resolve("settings");
+const a = c.resolve("ansi");
 
 // 2: CLI Argument Parsing
 let args = process.argv.slice(2); // Slice out the node binary name and the filename
@@ -40,7 +41,7 @@ for(let i = 0; i < args.length; i++) {
 
 // 4: Run
 if(extras.length < 1) {
-	console.error(`${ansi.fred}${ansi.hicol}Error: No subcommand specified.${ansi.reset}`);
+	console.error(`${a.fred}${a.hicol}Error: No subcommand specified.${a.reset}`);
 	show_help(settings);
 	process.exit();
 }
@@ -52,8 +53,8 @@ switch(extras[0]) {
 		break;
 	
 	default:
-		console.error(`${ansi.fred}${ansi.hicol}Error: Subcommand '${extras[0]}' not recognised.${ansi.reset}`);
-		console.error( `${ansi.fred}Perhaps you mistyped it, or it hasn't been implemented yet?`);
+		console.error(`${a.fred}${a.hicol}Error: Subcommand '${extras[0]}' not recognised.${a.reset}`);
+		console.error( `${a.fred}Perhaps you mistyped it, or it hasn't been implemented yet?`);
 		process.exit(1);
 		break;
 }
diff --git a/settings.toml b/settings.toml
new file mode 100644
index 0000000..b8b7960
--- /dev/null
+++ b/settings.toml
@@ -0,0 +1,2 @@
+# Custom settings file. This file overrides server/settings.default.toml - refer there of example of settings you can override.
+
