-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
299 lines (255 loc) · 8.63 KB
/
app.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/****************************************************************************
**
** Copyright (C) 2015
** Gustavo Valiati <[email protected]>
** Thiago R. M. Bitencourt <[email protected]>
**
** This file is part of the FishMonitoring project
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; version 2
** of the License.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
****************************************************************************/
/**
* Main app module [ app.js ]
* @namespace Main
*/
/**
* Holds the backend base code path
* @memberof Main
* @type string
*/
global.__base = __dirname + '/server/';
/**
* Holds the app execution type between development and production.
* @memberof Main
* @type boolean
*/
global.__DevEnv = false;
////////////////////////////////////
// Keep as first requirements >>> //
////////////////////////////////////
var Logs = require(__base + 'utils/logs');
var logger = require('winston');
/////////////////////////////////////////////
// <<< end of 'Keep as first requirements' //
/////////////////////////////////////////////
//Requirements
var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var https = require('https');
var fs = require('fs');
var Cors = require('cors');
var session = require('client-sessions');
var passport = require('passport');
var args = process.argv;
//Default logging options.
var debugConsole = false,
debugFile = false,
sillyConsole = false,
sillyFile = false;
// Verify parameters for logs configuration
if(args.indexOf('--debugAll') > -1){
debugConsole = true;
debugFile = true;
}else{
if(args.indexOf('--debugConsole') > -1){
debugConsole = true;
}else if (args.indexOf('--sillyConsole') > -1){
sillyConsole = true;
}
if(args.indexOf('--debugFile') > -1){
debugFile = true;
}else if (args.indexOf('--sillyFile') > -1){
sillyFile = true;
}
}
//Initialize the Logging module.
new Logs(debugConsole, debugFile, sillyConsole, sillyFile);
if(args.indexOf('--dev') > -1){
global.__DevEnv = true;
logger.warn('Starting on development mode');
}
//Synchronize the Sequelize Models (static and dynamic) to the database
var SynchronizeDb = require(__base + 'controller/database/synchronizedb');
SynchronizeDb.start(function(err){
//When the synchronization is finished, this current callback is triggered.
if(err){
logger.error("Erro to initialize Database: " + err);
return 1;
}
//Load Components only after logger had started
var Server = require(__base + 'controller/collector/server');
var LoadRouter = require(__base + 'routes/loadroutes');
var LoadLoginRouter = require(__base + 'routes/loadloginroute');
var tokenAuthentication = require(__base + 'controller/tokenauthentication');
var createDefaults = require(__base + 'controller/database/createdefaults');
//Create default credentials if no user is found
createDefaults(function(err){
if(err)
throw new Error('Error on create default credentials: ' + err);
});
//Clean restricted_media directory
var cleanErrors = require(__base + 'utils/cleanrestrictedmedia')();
if(cleanErrors){
logger.error('Not able to clean restricted_media directory: ' + cleanErrors);
return 1;
}
/**
* To generate cert files, type on terminal:
* openssl genrsa -out platform-key.pem 4096
* openssl req -new -key platform-key.pem -out platform-cert-req.csr
* openssl x509 -req -in platform-cert-req.csr -signkey platform-key.pem -out platform-cert.pem
*/
var options = {
key: fs.readFileSync(__base + 'config/ssl/platform-key.pem'),
cert: fs.readFileSync(__base + 'config/ssl/platform-cert.pem')
};
//Create and configure the express server.
/**
* Holds the Express Server
* @type {Object}
* @memberof Main
*/
var app = express();
app.use(Cors());
app.use(bodyParser.json({type: 'application/json'}));
app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
app.use(passport.initialize());
app.use(session({
cookieName: 'appSession',
secret: 'eg[isfd-8yF9-7w2315df{}+Ijsli;;to8',
duration: 60 * 60 * 1000, //keep session for 60 minutes
activeDuration: 10 * 60 * 1000,
secure: true
}));
//Needed headers for clients access.
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Credentials', 'true');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
//Log every request
app.all('*', function(req, res, next){
logger.debug("Method " + req.method + " for URL " + req.url);
next();
});
//Add to every request a new function called 'response'. This function handles a structed response.
app.all('*', function(req, res, next){
res.response = function(error, responseStatus, message){
var sendMessage = {message: message, status: responseStatus};
if(error){
sendMessage.error = error;
}
return res.status(responseStatus).send(sendMessage);
};
next();
});
/**
* This functions gets some erros like 'bodyParser errors'.
* To check if it is bodyparser error, remove the response below and just call next().
*/
app.use(function(err, req, res, next) {
if(err){
return res.status(400).send({message: "Something wrong with your object", error: err.toString(), status: 400});
}
next();
});
/**
* HTTP port number
* @type {Number}
* @memberof Main
*/
var httpPort = 8180;
/**
* HTTPS port number
* @type {Number}
* @memberof Main
*/
var httpsPort = 8143;
/**
* Holds the path for the login route.
* @type {String}
*/
var loginPath = '/login';
/**
* Holds the path for the api route.
* @type {String}
*/
var apiPath = '/api';
/**
* Holds the path for the web app route.
* @type {String}
*/
var webPath = global.__DevEnv? '/web-dev' : '/web';
//Load the application routes
var apiRoutes = new LoadRouter(apiPath);
var login = new LoadLoginRouter();
// Redirect any connection on http to https (secure)
app.use('*', function(req, res, next){
if(!req.secure){
var host = req.headers.host.split(':')[0];
return res.redirect('https://' + host + req.originalUrl);
}
next();
});
//Redirect any request on the root path to the web application.
app.get('/', function(req, res){
res.redirect(webPath);
});
// Middleware that decides if a request is going to be redirected to the login page if there is not session.
var redirectMidler = function(req, res, next){
if(req.originalUrl.indexOf(webPath) !== -1){
return login.hasSession(req)? next() : res.redirect(loginPath);
}else if(req.originalUrl.indexOf(loginPath) !== -1){
return login.hasSession(req)? res.redirect(webPath): next();
}else{
logger.error("Unknown Location");
return res.status(404).send({message: "Unknown Location"});
}
};
//Staticaly serves the api documentation web page.
app.use('/api/doc', express.static(__dirname + '/apidoc'));
//Configures the Bearer authentication method for the api routes
var authenticate = new tokenAuthentication(app);
authenticate.useBearer(apiPath);
//Setup the session validation for web access.
app.use(webPath, redirectMidler);
app.use(loginPath, redirectMidler);
if(global.__DevEnv){
//If running in dev environment, uses the source from web-dev for the web application.
app.use(webPath, express.static('web-dev/private'));
app.use(loginPath, express.static('web-dev/public/login'));
}else{
//Otherwise, uses the /web folder, where the deploy process have been done.
app.use(webPath, express.static('web/private'));
app.use(loginPath, express.static('web/public/login'));
}
//Set under 'apiPath' path every 'apiRoute' route
app.use(apiPath, apiRoutes);
//Set under root path the login routes.
app.use(login.routes);
//Create and start the tcp server for communicating to the collectors
var server = new Server();
server.startServer();
https.createServer(options, app).listen(httpsPort, function(){
logger.info("HTTPS server listening on port %s", httpsPort);
});
http.createServer(app).listen(httpPort, function(){
logger.info("HTTP server listening on port %s", httpPort);
});
});