-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
server.js
61 lines (49 loc) · 1.81 KB
/
server.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
'use strict';
const { node } = require('./lib/dependencies.js');
const { fsp, path } = node;
const application = require('./lib/application.js');
const Config = require('./lib/config.js');
const Logger = require('./lib/logger.js');
const Database = require('./lib/database.js');
const Server = require('./lib/server.js');
const Channel = require('./lib/channel.js');
const initAuth = require('./lib/auth.js');
const loadCert = async (certPath) => {
const key = await fsp.readFile(path.join(certPath, 'key.pem'));
const cert = await fsp.readFile(path.join(certPath, 'cert.pem'));
return { key, cert };
};
const main = async () => {
const configPath = path.join(application.path, 'config');
const config = await new Config(configPath);
const logPath = path.join(application.root, 'log');
const logger = await new Logger(logPath);
Object.assign(application, { config, logger });
const logError = (err) => {
logger.error(err ? err.stack : 'No exception stack available');
};
process.on('uncaughtException', logError);
process.on('warning', logError);
process.on('unhandledRejection', logError);
const certPath = path.join(application.path, 'cert');
const cert = await loadCert(certPath).catch(() => {
logError(new Error('Can not load TLS certificates'));
});
application.db = new Database(config.database);
application.auth = initAuth();
application.sandboxInject('auth', application.auth);
const { ports } = config.server;
const options = { cert, application, Channel };
for (const port of ports) {
application.server = new Server(config.server, options);
logger.system(`Listen port ${port}`);
}
await application.init();
logger.system('Application started');
const stop = () => {
process.exit(0);
};
process.on('SIGINT', stop);
process.on('SIGTERM', stop);
};
main();