-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
129 lines (98 loc) · 3.26 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
/**
* CHANGE IN MYSQL-SERVER
* PATH: etc/mysql/mysql.conf.d/mysqld.cnf
* nano /etc/mysql/conf.d/mysql.cnf
* [...]
* max_allowed_packet=10G
* key_buffer_size = 16M
* PARAM:
* max_allowed_packet = 100000000
* net_buffer_length = 1000000
*/
//check if in the arguments the debug mode enable
global.debug = process.argv.join('#').includes('APPDEBUG=')? process.argv.filter(arg => arg.includes('APPDEBUG=')).map(arg => parseInt(arg.split('=')[1], 10)>0)[0] : false
var logtimestamp = require('log-timestamp');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var compression = require('compression');
var timeout = require('connect-timeout')
var validator = require('express-validator');
var cors = require('cors');
var helmet = require('helmet');
var index = require('./routes/index');
var tracking = require('./routes/tracking');
var users = require('./routes/users');
var api = require('./routes/api');
var project = require('./routes/project');
var settings = require('./routes/settings');
var client = require('./routes/client');
var ioError = require('./module/IOHandler').error;
var log = require('./module/lib/log');
var limiter = require('./module/Limiter').limiter;
var banner = require('./module/Limiter').banner;
var redisLoader = require('./module/ControlListsRedisLoader');
var app = express();
app.use('/tracking/upload', banner);
app.use('/tracking/upload',limiter)
app.use(timeout('180s'))
app.use(log.request)
//for http security
app.use(helmet());
app.use(compression());
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(path.join(__dirname, 'public', 'a', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json({limit: '50mb', extended: false}));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false, parameterLimit:50000 }));
app.use(cookieParser());
app.use(cors({origin: '*'}));
app.use(validator());
app.use('/', index);
app.use('/tracking', tracking);
app.use('/users', users);
app.use('/api', api);
app.use('/project', project);
app.use('/settings', settings);
app.use('/client', client);
// catch 404 and forward to error handler
app.use((req, res, next) => {
if(req.hasOwnProperty('headers') &&
req.headers.hasOwnProperty('referer') &&
typeof req.headers.referer === 'string' &&
req.headers.referer.indexOf('/a/')<0){
var err = new Error('Page not Found');
err.status = 404;
next(err);
}else{
res.status(400).end();
}
});
// error handler
app.use(ioError);
process.on('unhandledRejection', async (type, promise, reason) => {
try {
if(typeof type == 'object'){
console.error("type %s", type.stack);
console.error("promise: %s", promise);
console.error("reason: %s", reason);
if(type.hasOwnProperty('stack')){
error = type.stack.toString();
}else{
error = type
}
}else{
error = type;
}
log.error(error);
} catch (err) {
console.error(err);
log.error(err);
}
});
//checks if redis databases contain values, if not, it loads them from the mysql tables
redisLoader.loadCL2RedisIfEmpty();
module.exports = app;