-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathall_servers.js
More file actions
94 lines (78 loc) · 2.47 KB
/
all_servers.js
File metadata and controls
94 lines (78 loc) · 2.47 KB
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
// Copyright Titanium I.T. LLC.
import * as ensure from "util/ensure.js";
import { CommandLine } from "infrastructure/command_line.js";
import { HttpServer } from "http/http_server.js";
import { Log } from "infrastructure/log.js";
import { WwwRouter } from "./www/www_router.js";
import { Rot13Router } from "./rot13_service/rot13_router.js";
/** Application startup (parse command line and start servers). */
export class AllServers {
/** Only for use by tests. The command-line interface's "usage" string. */
static get USAGE() {
return "Usage: run [www server port] [rot-13 server port]";
}
/**
* Factory method. Creates the servers, but doesn't start them.
* @returns {AllServers} the servers
*/
static create() {
ensure.signature(arguments, []);
return new AllServers(
Log.create(),
CommandLine.create(),
HttpServer.create(),
HttpServer.create(),
);
}
/** Only for use by tests. (Use the factory method instead.) */
constructor(log, commandLine, wwwServer, rot13Server) {
ensure.signature(arguments, [ Log, CommandLine, HttpServer, HttpServer ]);
this._log = log;
this._commandLine = commandLine;
this._wwwServer = wwwServer;
this._rot13Server = rot13Server;
}
/**
* Parse the command line and start the servers.
*/
async startAsync() {
ensure.signature(arguments, []);
const args = this._commandLine.args();
try {
const { wwwPort, rot13Port } = parseArgs(args);
await this.#startServersAsync(wwwPort, rot13Port);
}
catch (err) {
logStartupError(this._log, args, err);
}
}
async #startServersAsync(wwwPort, rot13Port) {
const wwwLog = this._log.bind({ node: "www" });
const rot13Log = this._log.bind({ node: "rot13" });
this._wwwRouter = WwwRouter.create(wwwLog, rot13Port);
this._rot13Router = Rot13Router.create(rot13Log);
await Promise.all([
this._wwwServer.startAsync(wwwPort, wwwLog, this._wwwRouter),
this._rot13Server.startAsync(rot13Port, rot13Log, this._rot13Router),
]);
}
}
function parseArgs(args) {
if (args.length !== 2) throw new Error(`invalid command-line arguments (${AllServers.USAGE})`);
return {
wwwPort: parse(args[0], "www server"),
rot13Port: parse(args[1], "ROT-13 server"),
};
function parse(arg, name) {
const result = parseInt(arg, 10);
if (Number.isNaN(result)) throw new Error(`${name} port is not a number`);
return result;
}
}
function logStartupError(log, args, err) {
log.emergency({
message: "startup error",
commandLineArguments: args,
error: err,
});
}