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 13
/
server.js
126 lines (100 loc) · 3.76 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var express = require('express');
var app = new express();
var https = require('https');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var fs = require('fs');
var path = require('path');
var config = require('./config/config');
var winston = require('winston');
require('winston-daily-rotate-file');
var routeBuilder = require('./routeBuilder');
var socketio = require('socket.io');
//set up logging
var logger = new(winston.Logger)({
level: config.logging.logLevel,
transports: [
new(winston.transports.Console)(),
new(winston.transports.DailyRotateFile)({
filename: config.logging.logFile,
prepend: true
})
]
});
logger.info('Firing up QMC Utilities', {
module: 'server'
});
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cookieParser());
if (!config.thisServer.devMode) {
var rewrite = require("./middleware/rewrite");
var auth = require('./middleware/auth');
var vpList = require('./middleware/vpList');
app.use(vpList());
app.use(rewrite());
app.use(auth());
}
app.use('/qmcu/public', express.static(config.thisServer.publicPath));
app.use('/qmcu/bower_components', express.static(config.thisServer.bowerPath));
app.use('/qmcu/node_modules', express.static(config.thisServer.nodeModulesPath));
app.use('/qmcu/data', express.static(config.thisServer.dataPath));
app.use('/qmcu/app', express.static(config.thisServer.appPath));
app.use('/qmcu/plugins', express.static(config.thisServer.pluginPath));
app.use('/*/qmcu/public', express.static(config.thisServer.publicPath));
app.use('/*/qmcu/bower_components', express.static(config.thisServer.bowerPath));
app.use('/*/qmcu/node_modules', express.static(config.thisServer.nodeModulesPath));
app.use('/*/qmcu/data', express.static(config.thisServer.dataPath));
app.use('/*/qmcu/app', express.static(config.thisServer.appPath));
app.use('/*/qmcu/plugins', express.static(config.thisServer.pluginPath));
logger.info(config.thisServer.appPath);
var menu = [];
routeBuilder.forEach(function(route, index) {
app.use('/*/qmcu/' + route.name, require(route.path));
app.use('/qmcu/' + route.name, require(route.path));
//build menu
menu.push(route);
});
app.use(function(req, res, next) {
res.locals.menu = menu;
next();
});
fs.writeFileSync(config.thisServer.dataPath + '/menu.json', JSON.stringify(menu));
var routes = require('./routes/routes');
app.use('/*/qmcu', routes);
app.use('/qmcu', routes);
var httpsOptions = {}
if (config.thisServer.hasOwnProperty("certificates")) {
if (config.thisServer.certificates.server !== undefined) {
//pem files in use
httpsOptions.cert = fs.readFileSync(config.thisServer.certificates.server);
httpsOptions.key = fs.readFileSync(config.thisServer.certificates.server_key);
}
if (config.thisServer.certificates.pfx !== undefined) {
httpsOptions.pfx = fs.readFileSync(config.thisServer.certificates.pfx);
httpsOptions.passphrase = config.thisServer.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.thisServer.port, function() {
logger.info('QMCUtilities started', {
module: 'server'
});
//console.log(app._router.stack);
});
var io = new socketio(server);
io.on('connection', function(socket) {
routeBuilder.forEach(function(route, index) {
if (route.useSocketIO) {
socket.on(route.name, function(msg) {
console.log(route.name + "::" + msg);
io.emit(route.name, msg);
});
}
});
});