-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.js
87 lines (69 loc) · 2.38 KB
/
frontend.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
/**
* Created by kaloyan on 6.7.2016 г..
*/
'use strict';
const net = require('net');
const amqplib = require('amqplib');
const winston = require('winston');
const models = require('./models');
const { Etcd3 } = require('etcd3');
const ConnectionService = require('./services/connection');
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.netServer = null;
global.connectionService = new ConnectionService();
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', () => {
global.connectionService.closeAllConnections();
if (global.netServer) {
global.netServer.close();
}
});
promises.push(amqplib.connect(amqpUrl));
promises.push(models.sequelize.sync());
Promise.all(promises).then(([conn, db]) => {
global.amqpConnection = conn;
if (process.env.NODE_ENV === 'development') {
const tls = require('tls');
const fs = require('fs');
global.netServer = tls.createServer({
key: fs.readFileSync('certs/tls.key'),
cert: fs.readFileSync('certs/tls.crt')
}, (socket) => {
global.connectionService.onNewConnection(socket);
});
}
else {
global.netServer = net.createServer((socket) => {
global.connectionService.onNewConnection(socket);
});
}
global.netServer.listen(1119, () => {
global.logger.info('Listening on port 1119');
});
global.netServer.on('close', () => {
global.etcd3.close();
if (global.amqpConnection) {
global.amqpConnection.close();
}
});
});