-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (50 loc) · 1.87 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
62
63
64
65
/**
* Created by kaloyan on 6.7.2016 г..
*/
'use strict';
const amqplib = require('amqplib');
const winston = require('winston');
const models = require('./models');
const { Etcd3 } = require('etcd3');
const ServiceReceiver = require('./messaging/receiver');
const winstonFileTransportConfig = require('./config/winston.json');
const winstonFileTransport = new winston.transports.File(winstonFileTransportConfig);
let winstonTransports = [winstonFileTransport];
if (process.env.NODE_ENV === 'development') {
const winstonConsoleTransport = new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
});
winstonTransports.push(winstonConsoleTransport);
}
winston.emitErrs = true;
global.amqpConnection = null;
global.serviceReceiver = null;
global.logger = new winston.Logger({transports: winstonTransports, exitOnError: false});
global.etcd3 = new Etcd3({hosts: process.env.ETCD_HOST});
let amqpUrl = 'amqp://' + process.env.RABBITMQ_USERNAME + ':' + process.env.RABBITMQ_PASSWORD + '@' + process.env.RABBITMQ_SERVICE_NAME;
let promises = [];
process.on('unhandledRejection', (error) => {
global.logger.error(error);
process.exit(1);
});
process.on('SIGINT', () => {
if (global.serviceReceiver) {
global.serviceReceiver.closeChannel().then(() => {
if (global.amqpConnection) {
global.amqpConnection.close();
}
});
}
global.etcd3.close();
});
promises.push(amqplib.connect(amqpUrl));
promises.push(models.sequelize.sync());
Promise.all(promises).then(([conn]) => {
global.amqpConnection = conn;
const ServiceName = process.argv[2];
const Service = require('./services/' + ServiceName);
global.serviceReceiver = new ServiceReceiver(new Service());
});