This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
95 lines (82 loc) · 2.2 KB
/
app.js
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
95
const program = require('commander');
const Api = require('./src/api/api');
const ApiHandler = require('./src/api/api-handler');
const Peer = require('./src/peer/peer');
const PeerListener = require('./src/peer/peer-listener');
const ApiLocalProxy = require('./src/peer/api/api-local-proxy');
const ApiRemoteProxy = require('./src/peer/api/api-remote-proxy');
/**
* Encapsulates application
*/
class App {
constructor(mode) {
this.mode = mode || 'BUNDLED';
}
/**
* Start application
* @return {Promise<void>}
*/
async start() {
switch (this.mode) {
case 'BUNDLED':
await this.startBundled();
return;
case 'API': {
await this.startApi();
return;
}
case 'PEER': {
await this.startPeer();
break;
}
default: {
process.exit(1);
}
}
}
/**
* Start the API withou Peer
* @return {Promise<void>}
*/
async startApi() {
const api = new Api();
await api.start();
}
/**
* Start Peer without the API
* @return {Promise<void>}
*/
async startPeer() {
const apiRemoteProxy = new ApiRemoteProxy();
const peer = new Peer(apiRemoteProxy);
const peerListener = new PeerListener(peer, apiRemoteProxy);
await peer.start();
peerListener.start();
}
/**
* Start the API and Peer in the same process
* @return {Promise<void>}
*/
async startBundled() {
const apiHandler = new ApiHandler();
const api = new Api(apiHandler);
const apiLocalProxy = new ApiLocalProxy(apiHandler);
const peer = new Peer(apiLocalProxy);
const peerListener = new PeerListener(peer, apiLocalProxy);
await api.start();
await peer.start();
peerListener.start();
}
}
program
.option('-a, --api-callback-url <url>', 'API callback URL when mode is PEER')
.option('-e, --execution-mode <mode>', 'app execution mode (API|PEER|BUNDLED)');
program.parse(process.argv);
process.env.API_CALLBACK_URL = program.apiCallbackUrl || process.env.API_CALLBACK_URL;
const mode = program.executionMode || process.env.EXECUTION_MODE;
const app = new App(mode);
app.start().catch((e) => {
// eslint-disable-next-line no-console
console.error(e);
process.exit(1);
});