-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
119 lines (100 loc) · 3.29 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
// Requiring and configuring dotenv for using environment variables
require('dotenv').config();
// Node modules
const http = require('http');
const fs = require('fs');
const path = require('path');
// NPM modules
const express = require('express');
const spdy = require('spdy');
const logger = require('./lib/logger');
// Importing the services config
const integrated_services = require('./services-config');
// Importing all services
const mms = require('./services/mms');
// Initializing app
const app = express();
// Initialize httpServer and http2Server objects
let httpServer = null;
let http2Server = null;
// Set the port
app.set('port', process.env.PORT || 8000);
// Add the main route
app.get('/', function (req, res) {
res.send({
message: 'Dynamic Integration Service is up and running.'
});
});
// Setting up HTTP server
if (process.env.HTTP_ENABLED === 'true') {
httpServer = http.createServer(app);
// Run HTTP Server
httpServer.listen(app.get('port'), () => {
logger.info('Dynamic Integration Service is running on ' + app.get('port'))
});
}
// Setting up HTTP/2 (HTTPS) server
if (process.env.HTTPS_ENABLED === 'true') {
// setting the options
const privateKey = fs.readFileSync(path.join(process.cwd(), process.env.SSLKEY), 'utf8');
const certificate = fs.readFileSync(path.join(process.cwd(), process.env.SSL_CERT), 'utf8');
const options = {
key: privateKey,
cert: certificate,
protocol: ['h2'],
honorCipherOrder: true,
requestCert: true,
rejectUnauthorized: true,
minVersion: 'TLSv1.2',
maxVersion: 'TLSv1.2'
};
// creating the https server
http2Server = spdy.createServer(options, app);
// Run HTTPS Server
http2Server.listen(app.get('port'), () => {
logger.info("Dynamic Integration Service is running on " + app.get('port'));
});
}
// Publisher
const publisher = require('./pubsub/publisher');
// Subscriber
const subscriber = require('./pubsub/subscriber');
// INITIALIZING all services
const serviceClasses = {
mms: new mms(publisher)
}
/**
* Inital Start Function
*/
function init() {
// Subscribe to channels
const channels = ['AUTH_INTEGRATION'];
// storing integrated services key to redis
publisher.set('INTEGRATED_SERVICES', JSON.stringify(integrated_services));
// Subscribe to channels
subscriber.subscribe(channels, (err, count) => {
logger.info(`Subscribed to ${count} channels`);
});
handleMessages();
}
function handleMessages() {
// Listen to all messages and route to correct service
subscriber.on('message', function(channel, message) {
logger.info(`Receive message ${message} from channel ${channel}`);
// Each message should have the service name in it. If not, error will be thrown
// If service does not exist, error will be thrown
switch(channel) {
case 'AUTH_INTEGRATION':
const parsedMessage = JSON.parse(message);
if (parsedMessage.name) {
serviceClasses[parsedMessage.name].handleAuth(message);
}
else {
logger.error('No property "name" for AUTH_INTEGRATION');
}
break;
}
});
}
init();