This repository has been archived by the owner on May 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
server.js
95 lines (73 loc) · 2.89 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
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
//server.js
//BASE Setup
//=========================================================================
//require statements
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var config = require('./config/config');
var Promise = require('bluebird');
var doWork = require('./lib/dowork');
var fs = require('fs');
var path = require('path');
var https = require('https');
var socketio = require('socket.io');
var logger = require('./lib/logger');
var notifyFactory = require('./lib/notifyFactory');
var x = {};
logger.info('Firing up the Governed Metrics Service ReST API', { module: 'server' });
notifyFactory.checkQRSConnection()
.then(function(result) {
if (result === "SUCCESS") {
return launchServer();
} else {
logger.error(JSON.stringify(result), { module: "server" });
process.exit();
}
})
.catch(function(error) {
logger.error("Shutting down GMS. Can't Start Up");
logger.error(JSON.stringify(error), { module: "server" });
process.exit();
});
function launchServer() {
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/masterlib/public', express.static(config.gms.publicPath));
app.use('/masterlib/node_modules', express.static(config.gms.nodeModPath));
app.use('/masterlib/docs', express.static(config.gms.docsPath));
logger.info('Setting port', { module: 'server' });
var port = config.gms.port || 8590;
logger.info('Setting route', { module: 'server' });
var popmasterlib = require('./routes/routes');
//Register routes
//all routes will be prefixed with api
app.use('/masterlib', popmasterlib);
//Start the server
var httpsOptions = {}
if (config.gms.hasOwnProperty("certificates")) {
if (config.gms.certificates.server !== undefined) {
//pem files in use
httpsOptions.cert = fs.readFileSync(config.gms.certificates.server);
httpsOptions.key = fs.readFileSync(config.gms.certificates.server_key);
}
if (config.gms.certificates.pfx !== undefined) {
httpsOptions.pfx = fs.readFileSync(config.gms.certificates.pfx);
httpsOptions.passphrase = config.gms.certificates.passphrase;
}
} else {
httpsOptions.cert = fs.readFileSync(config.certificates.server),
httpsOptions.key = fs.readFileSync(config.certificates.server_key)
}
var server = https.createServer(httpsOptions, app);
server.listen(config.gms.port, function() {
logger.info('Governed Metrics Service version ' + config.gms.version + ' started', { module: 'server' });
});
var io = new socketio(server);
io.on('connection', function(socket) {
socket.on("gms", function(msg) {
console.log("gms" + "::" + msg);
io.emit("gms", msg);
});
});
}