-
Notifications
You must be signed in to change notification settings - Fork 7
/
mhub-server.ts
94 lines (79 loc) · 2.12 KB
/
mhub-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* MHub server (mserver)
*
* Makes MHub pubsub Nodes available through WebSockets.
*/
import "source-map-support/register";
import * as path from "path";
import * as yargs from "yargs";
import parseConfigFile from "./configparser";
import { LogLevel } from "./logger";
import { LoggingOptions, MServer } from "./nodeserver";
import log from "./log";
// tslint:disable-next-line:no-shadowed-variable
function die(fmt: string, ...args: any[]): void {
log.fatal(fmt, ...args);
process.exit(1);
}
// Build list of valid log level names (e.g. none, fatal, error, ...)
const logLevelNames = Object.keys(LogLevel)
.filter((s) => !/\d+/.test(s))
.map((s) => s.toLowerCase());
// Parse input arguments
const args = yargs
.usage("mhub-server [-c <config_file>]")
.help("help")
.alias("h", "help")
// tslint:disable-next-line:no-require-imports
.version()
.alias("v", "version")
.option("config", {
type: "string",
alias: "c",
description:
"Filename of config, uses mhub's server.conf.json by default",
})
.option("loglevel", {
type: "string",
alias: "l",
description:
"Override log level in config file. Valid options: " +
logLevelNames.join(", "),
})
.strict().argv;
// Parse config file
let configFile: string;
if (!args.config) {
configFile = path.resolve(__dirname, "../../server.conf.json");
} else {
configFile = path.resolve(args.config);
}
function setLogLevel(logLevelName: LoggingOptions | string) {
// Convert config.logging to a LogLevel
const matching = Object.keys(LogLevel).filter((s) => {
return s.toLowerCase() === logLevelName;
})[0];
if (matching) {
log.logLevel = (<any>LogLevel)[matching] as LogLevel;
} else {
die(
`Invalid log level '${logLevelName}', expected one of: ${logLevelNames.join(
", "
)}`
);
}
}
function main(): Promise<void> {
const config = parseConfigFile(configFile);
setLogLevel(args.loglevel || config.logging);
log.info("Using config file " + configFile);
// Create server
const server = new MServer(config);
server.setLogger(log);
return server.init();
}
Promise.resolve()
.then(main)
.catch((err: Error) => {
die("main failed: ", err);
});