191 lines
5.1 KiB
TypeScript
191 lines
5.1 KiB
TypeScript
/**
|
|
* @author Martin Karkowski
|
|
* @email m.karkowski@zema.de
|
|
* @create date 2020-11-11 13:27:58
|
|
* @modify date 2020-12-03 14:00:03
|
|
* @desc [description]
|
|
*/
|
|
|
|
import { ArgumentParser } from "argparse";
|
|
import { readFile } from "fs/promises";
|
|
import "reflect-metadata";
|
|
import { AmqpLayer } from "../communication/amqpLayer";
|
|
import { EventLayer } from "../communication/eventLayer";
|
|
import { IoSocketClient } from "../communication/IoSocketClient";
|
|
import { IoSocketServer } from "../communication/IoSocketServer";
|
|
import { getPackageLoader } from "../loader/getPackageLoader";
|
|
import { loadFunctions, loadPackages } from "../loader/loadPackages";
|
|
import { getNopeLogger } from "../logger/getLogger";
|
|
import { LoggerLevels } from "../logger/nopeLogger";
|
|
import { setGlobalLoggerLevel } from "../logger/setGlobalLoggerLevel";
|
|
import { INopePackageLoader } from "../types/nope/nopePackageLoader.interface";
|
|
import { NOPELOGO } from "./renderNope";
|
|
|
|
console.log(NOPELOGO);
|
|
console.log("\n\n");
|
|
|
|
// Define the Main Function.
|
|
// This function is used as cli tool.
|
|
export async function runNopeBackend() {
|
|
const parser = new ArgumentParser({
|
|
version: "1.0.0",
|
|
addHelp: true,
|
|
description: "Command Line interface, determines the available Packages."
|
|
});
|
|
|
|
const validLayers = {
|
|
event: EventLayer,
|
|
amqp: AmqpLayer,
|
|
"io-server": IoSocketServer,
|
|
"io-client": IoSocketClient
|
|
};
|
|
|
|
const layerDefaults = {
|
|
event: [],
|
|
amqp: ["localhost"],
|
|
"io-server": [7000],
|
|
"io-client": ["http://localhost:7000"]
|
|
};
|
|
|
|
let opts: {
|
|
params: any[];
|
|
};
|
|
|
|
try {
|
|
// Try to read in the default config file.
|
|
opts = JSON.parse(
|
|
await readFile("./nopeconfig.json", {
|
|
encoding: "utf-8"
|
|
})
|
|
);
|
|
} catch (error) {
|
|
opts = {} as any;
|
|
}
|
|
|
|
parser.addArgument(["-f", "--file"], {
|
|
help: "File containing containing the package definitions.",
|
|
defaultValue: "./config/settings.json",
|
|
type: "string",
|
|
dest: "file"
|
|
});
|
|
parser.addArgument(["-c", "--channel"], {
|
|
help:
|
|
"The Communication Channel, which should be used. Possible Values are: " +
|
|
// Display all Options:
|
|
Object.getOwnPropertyNames(validLayers)
|
|
.map((item) => "\"" + item + "\"")
|
|
.join(", "),
|
|
defaultValue: "event",
|
|
type: "string",
|
|
dest: "channel"
|
|
});
|
|
parser.addArgument(["-p", "--params"], {
|
|
help:
|
|
"Paramas for the Channel, to connect to. The Following Defaults are used: \n" +
|
|
JSON.stringify(layerDefaults, undefined, 4),
|
|
defaultValue: "not-provided",
|
|
type: "string",
|
|
dest: "params"
|
|
});
|
|
parser.addArgument(["-s", "--skipLoadingConfig"], {
|
|
help: "Flag to prevent loading the elements defined in the settings.json.",
|
|
action: "append",
|
|
nargs: "?",
|
|
dest: "skipLoadingConfig"
|
|
});
|
|
parser.addArgument(["-l", "--log"], {
|
|
help:
|
|
"Specify the Logger Level. Defaults to \"info\". Valid values are: " +
|
|
LoggerLevels.join(", "),
|
|
defaultValue: "silly",
|
|
type: "string",
|
|
dest: "log"
|
|
});
|
|
|
|
const args = parser.parseArgs();
|
|
|
|
if (LoggerLevels.includes(args.log)) {
|
|
setGlobalLoggerLevel(args.log);
|
|
}
|
|
|
|
args.skipLoadingConfig = Array.isArray(args.skipLoadingConfig);
|
|
|
|
// Define a Logger
|
|
const logger = getNopeLogger("starter");
|
|
|
|
if (!Object.getOwnPropertyNames(validLayers).includes(args.channel)) {
|
|
logger.error(
|
|
"Invalid Channel. Please use the following values. " +
|
|
Object.getOwnPropertyNames(validLayers)
|
|
.map((item) => "\"" + item + "\"")
|
|
.join(", ")
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Assign the Default Setting for the Channel.
|
|
opts.params = layerDefaults[args.channel];
|
|
|
|
if (args.params != "not-provided") {
|
|
try {
|
|
opts.params = JSON.parse(args.params);
|
|
} catch (e) {
|
|
logger.error(
|
|
"Unable to parse the Parameters for the channel. Please use valid JSON!"
|
|
);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// If required load all Packages.
|
|
if (!args.skipLoadingConfig) {
|
|
// Try to load the Modules.
|
|
try {
|
|
logger.info("loading Functions");
|
|
await loadFunctions(args.file);
|
|
} catch (e) {
|
|
logger.error(
|
|
"Unable to load the Packages defined in " +
|
|
args.file +
|
|
" See Output for detailled information",
|
|
e
|
|
);
|
|
return;
|
|
}
|
|
}
|
|
|
|
let loader: INopePackageLoader;
|
|
try {
|
|
loader = getPackageLoader({
|
|
communicator: new validLayers[args.channel](...opts.params),
|
|
logger: getNopeLogger("dispatcher", "debug")
|
|
});
|
|
} catch (e) {
|
|
getNopeLogger("cli", "info").error("failed to load the Packages", e);
|
|
}
|
|
|
|
// If required load all Packages.
|
|
if (!args.skipLoadingConfig) {
|
|
// Try to load the Modules.
|
|
try {
|
|
logger.info("loading Packages");
|
|
await loadPackages(loader, args.file);
|
|
} catch (e) {
|
|
logger.error(
|
|
"Unable to load the Packages defined in " +
|
|
args.file +
|
|
" See Output for detailled information",
|
|
e
|
|
);
|
|
return;
|
|
}
|
|
}
|
|
|
|
return loader.dispatcher;
|
|
}
|
|
|
|
// If requested As Main => Perform the Operation.
|
|
if (require.main === module) {
|
|
runNopeBackend().catch(console.error);
|
|
}
|