Get dependency injection (basically) working. Hooray!

This commit is contained in:
Starbeamrainbowlabs 2019-05-23 22:26:31 +01:00
parent 15a5bf9d03
commit 0d3edd0a93
6 changed files with 37 additions and 31 deletions

View file

@ -75,6 +75,4 @@ class Ansi {
}
}
const ansi = new Ansi();
export default ansi;
export default Ansi;

View file

@ -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),
});

View file

@ -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;

View file

@ -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)
`);
};

View file

@ -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;
}

2
settings.toml Normal file
View file

@ -0,0 +1,2 @@
# Custom settings file. This file overrides server/settings.default.toml - refer there of example of settings you can override.