-
Notifications
You must be signed in to change notification settings - Fork 0
/
zimbradmin.js
134 lines (130 loc) · 4.42 KB
/
zimbradmin.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
127
128
129
130
131
132
133
134
'use strict';
// Chargement des modules de Node.js
var fs = require('fs');
var system = require('os');
global.bcrypt = require('bcrypt-nodejs');
var ZMConf = require('nconf');
ZMConf.env().file({file: 'server-config.json'});
var bunyan = require('bunyan');
global.log = bunyan.createLogger({name: 'ZM'});
var monitor = require('./ZMModules/monitor.js');
var ServerConfig = ZMConf.get("ServerConfig");
var ExtDirectConfig = ZMConf.get("ExtDirectConfig");
var MySQLConfig = ZMConf.get("MySQLConfig");
var express = require('express');
var https = require('https');
var http = require('http');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var compression = require('compression');
var bodyParser = require('body-parser');
var extdirect = require('extdirect');
global.mysql = require('mysql');//a supprimer par la suite
var mysql = require('mysql');
var async = require('async');
var HTTPServer; // serveur web http
var HTTPSServer; // serveur web https
var sslOpts = {
key: fs.readFileSync('./ssl/certif.key'),
cert: fs.readFileSync('./ssl/certif-crt.pem')
};
global.Monitor={};
global.pool = mysql.createPool({
connectionLimit: 100,
host: MySQLConfig.host,
user: MySQLConfig.user,
password: MySQLConfig.password,
database: MySQLConfig.database,
debug: false,
multipleStatements: true
});
monitor.init(ZMConf.get("Monitor"));
var notFound = function (req, res) {
res.setHeader('Content-Type', 'text/plain');
res.status(404).send('Page introuvable !');
console.log('page introuvable:');
console.log(req);
console.log('------------------------------------');
//next();
};
var startPage = function (req, res) {
res.sendFile('/client/desktop.html', {root: __dirname});
};
// définition des variables d'initialisation
//var port = 8080; // port d'ecoute
// création d'une application Express
var app = express();
// Configurations de l'application Express
// on lance les middleware de facon async afin d'accelerer
// le traitement fait par le serveur node.
function parallel(middlewares) {
return function (req, res, next) {
async.each(middlewares, function (mw, cb) {
mw(req, res, cb);
}, next);
};
}
;
app.use(parallel([
express.static(__dirname + '/client'),
//compression(),
bodyParser.json(),
cookieParser(),
session({
secret: ServerConfig.sessionSecret,
resave: false,
saveUninitialized: false,
cookie: {maxAge: ServerConfig.sessionMaxAge, secret: ServerConfig.sessionSecret},
rolling: true
})
]));
// on charge la page par defaut
app.get('/', startPage);
var directApi = extdirect.initApi(ExtDirectConfig);
var directRouter = extdirect.initRouter(ExtDirectConfig);
app.get(ExtDirectConfig.apiUrl, function (req, res) {
try {
directApi.getAPI(
function (api) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(api);
}, req, res
);
} catch (e) {
console.log(e);
}
});
// on ignore les GET requests sur le class path de extdirect
app.get(ExtDirectConfig.classRouteUrl, function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({success: false, msg: 'Unsupported method. Use POST instead.'}));
});
// POST request process route and calls class
app.post(ExtDirectConfig.classRouteUrl, function (req, res) {
if (req.session.userinfo ||req.body.action === "core.DXLogin") {
directRouter.processRoute(req, res);
} else
{
res.writeHead(401, {'Content-Type': 'application/json'});
res.end(JSON.stringify({success: false, msg: 'Please Login before'}));
}
});
//création serveur http et redirection vers https
HTTPServer=http.createServer(function(req, res) {
res.writeHead(301, {
Location: "https://"+ServerConfig.host+":"+ServerConfig.HTTPSPort.toString()
});
res.end();
}).listen(ServerConfig.HTTPPort);
// creation serveur https
HTTPSServer = https.createServer(sslOpts, app).listen(ServerConfig.HTTPSPort);
log.info('ZimbradminNG server listening on port %d in %s mode', ServerConfig.HTTPSPort, app.settings.env);
log.info('Running on :', system.hostname());
log.info('Node Version: ' + process.version);
// Écoute du signal SIGINT
process.on('SIGINT', function () {
log.info('Stopping Zimbradmin...');
HTTPServer.close();
HTTPSServer.close();
process.exit();
});