forked from WebbyLab/webbylab-starter-app-for-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.mjs
83 lines (63 loc) · 2.2 KB
/
app.mjs
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
import * as API from './lib/api/index.mjs';
import * as RestAPI from './lib/api/rest-api/app.mjs';
import * as JsonRPC from './lib/api/json-rpc/app.mjs';
import * as DomainModel from './lib/domain-model/index.mjs';
import Logger from './lib/infrastructure/Logger.mjs';
import EmailSender from './lib/infrastructure/notificator/Mail.mjs';
import UseCaseBase from './lib/use-cases/Base.mjs';
import config from './lib/config.cjs';
async function main() {
// Init infrastructure
const logger = new Logger();
const notificator = new EmailSender({
mailOptions : config.mail,
mainUrl : config.mainUrl
});
logger.info(`[App] Init Mode: ${process.env.MODE}`);
// Init Controllers Layer (API)
API.setLogger(logger);
RestAPI.start({ appPort: config.appPort });
await JsonRPC.start({ wssPort: config.wssPort });
// Init Domain Model Layer
const dbMode = process.env.MODE === 'application' ? 'db' : 'test-db';
const { sequelize } = DomainModel.initModels(config[dbMode]);
DomainModel.setLogger(logger);
// Init Use Cases Layer
UseCaseBase.setSequelizeInstanse(sequelize);
UseCaseBase.setNotificatorInstanse(notificator);
// Subscribe to system signals
process.on('SIGTERM', async () => {
logger.info('[App] SIGTERM signal catched');
await shutdown();
});
process.on('SIGINT', async () => {
logger.info('[App] SIGINT signal catched');
await shutdown();
});
process.on('unhandledRejection', error => {
console.error(error);
logger.fatal({
type : 'UnhandledRejection',
error : error.stack
});
});
process.on('uncaughtException', error => {
console.error(error);
logger.fatal({
type : 'UncaughtException',
error : error.stack
});
});
// Graceful shutdown
async function shutdown() {
await RestAPI.stop();
logger.info('[App] Closing sequelize connections');
await sequelize.close();
logger.info('[App] Exit');
process.exit(0);
}
}
main().catch((err) => {
console.error(err);
process.exit(1);
});