-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
279 lines (236 loc) · 8.79 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
'use strict';
console.time('Initialize Library');
const Async = require('async');
const Bcrypt = require('bcryptjs');
const bodyParser = require('body-parser');
const Crypto = require('crypto-js');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const ejs = require('ejs');
const express = require('express');
const expressSession = require('express-session');
const flash = require('connect-flash');
const fs = require('fs');
const helmet = require('helmet');
const logger = require('morgan');
const methodOverride = require('method-override');
const Moment = require('moment');
const mysql = require('mysql2');
const path = require('path');
const RateLimiter = require('limiter').RateLimiter;
const redis = require('redis');
const redisSession = require('connect-redis')(expressSession);
const Request = require('request');
const RequestPromise = require('request-promise');
const Sequelize = require('sequelize');
const SequelizeOp = Sequelize.Op;
const _ = require('underscore');
const userAgent = require('express-useragent');
const uuidv4 = require('uuid/v4');
const app = express();
const Limiter = new RateLimiter(50, 'minute');
const Router = express.Router();
require('dotenv').load();
console.timeEnd('Initialize Library');
const env = process.env;
console.time('Initialize App');
app.engine('html', ejs.renderFile);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.set('superSecret', env.APP_KEY);
app.use(logger('short'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded( { extended:true } ));
app.use(cookieParser(env.COOKIE_SECRET));
app.use('/', express.static(path.join(__dirname, 'public')));
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use(expressSession({ store: new redisSession({ host: env.REDIS_HOST, port: env.REDIS_PORT }), secret: env.SESSION_SECRET, saveUninitialized: true, resave: true }))
app.use(flash());
app.use(cors());
app.use(methodOverride());
app.use(helmet());
app.use(userAgent.express());
app.disable('x-powered-by');
app.locals.lang = 'en';
app.locals.currentYear = new Date().getFullYear();
console.timeEnd('Initialize App');
console.time('Initialize Redis');
let redisOptions = {
'host': env.REDIS_HOST,
'port': env.REDIS_PORT,
'prefix': env.REDIS_PREFIX
};
if(env.REDIS_PASSWORD){
redisOptions.password = env.REDIS_PASSWORD;
}
let RedisClient = redis.createClient(redisOptions);
RedisClient.on('error', (err) => {
console.error('ERROR ON CONNECTING TO REDIS');
});
console.timeEnd('Initialize Redis');
console.time('Initialize Database');
let sequelizeConnection = {
host: env.DB_HOST || '127.0.0.1',
port: env.DB_PORT || 3306,
database: env.DB_NAME,
username: env.DB_USERNAME,
password: env.DB_PASSWORD,
dialect: env.DB_ADAPTER || 'mysql',
operatorsAliases: SequelizeOp,
pool: {
max: 5,
min: 0,
idle: 10000
}
};
let sequelizeClient = new Sequelize(sequelizeConnection);
sequelizeClient.authenticate().then( () => {
console.log('Connection to database has been established successfully.');
}).catch( (err) => {
console.error(`ERROR WHEN CONNECTING TO DATABASE: ${err}`);
});
let databaseConnection = {
host: env.DB_HOST || '127.0.0.1',
port: env.DB_PORT || 3306,
database: env.DB_NAME,
user: env.DB_USERNAME,
password: env.DB_PASSWORD,
};
let DB = mysql.createConnection(databaseConnection);
console.timeEnd('Initialize Database');
let adminTemplate = 'admin/template.html';
let params = {
_, adminTemplate, app, Async, Bcrypt, Crypto, DB, env, fs, Limiter, Moment, path, RedisClient, Request, RequestPromise, Router, Sequelize, SequelizeOp, sequelizeClient, userAgent, uuidv4
};
let extendsObject = (target, source) => {
let key = Object.getOwnPropertyNames(source);
if(!target[key]) {
target[key] = source[key];
} else {
extendsObject(target[key], source[key]);
}
}
// Require Files
let objectCreation = (keys, type, fullPath, ext) => {
let keysLength = keys.length;
let tempObject = {};
if(ext == '.json') {
tempObject[keys[keysLength - 1]] = require(fullPath);
} else {
tempObject[keys[keysLength - 1]] = require(fullPath)(params);
}
for(let i = (keysLength - 2); i >= 0; i--) {
let newObject = {};
newObject[keys[i]] = tempObject;
tempObject = newObject;
}
if(!params.hasOwnProperty(type)) {
params[type] = {};
}
extendsObject(params[type], tempObject);
}
let requireFile = (path, type, ext = '.js') => {
let directoryFiles = fs.readdirSync(path);
for(let file of directoryFiles) {
let currentLoop = `${path}/${file}`;
let checkFile = fs.lstatSync(currentLoop);
if (checkFile.isFile()) {
let trimPath = currentLoop.replace(ext, '').replace(type, '').split('/');
let trimType = type.replace('./', '').replace('/', '');
objectCreation(trimPath, trimType, currentLoop, ext);
} else if (checkFile.isDirectory()) {
requireFile(currentLoop, type, ext);
}
}
};
// Require JSON Files in CONFIG Folder
let requireOneChildFolder = (path, type) => {
let directoryFiles = fs.readdirSync(path);
let requiredFile = {};
requiredFile[type] = {};
let fileExcept = ['routes'];
if(!params.hasOwnProperty(type)) {
params[type] = {};
}
for(let file of directoryFiles) {
let currentLoop = `${path}/${file}`;
let trimPath = currentLoop.replace('.json', '').replace(path + '/', '');
if(fileExcept.indexOf(trimPath ) >= 0) {
requiredFile[type][trimPath] = require(currentLoop);
} else {
requiredFile[type][trimPath] = require(currentLoop)[env.APP_ENV];
}
}
_.extend(params[type],requiredFile[type]);
}
console.time('Initialize Core');
console.time('Initialize Rules');
let rulesPath = './Rules';
requireFile(rulesPath, './Rules/', '.json');
console.timeEnd('Initialize Rules');
console.time('Initialize Config');
let configPath = './config';
requireOneChildFolder(configPath, 'CONFIG');
//set the app.local for able access in view
app.locals.config = params.CONFIG;
console.timeEnd('Initialize Config');
console.time('Initialize Helper');
let helper = require('./helper/helper.js')(params);
params.helper = helper;
app.locals.helpers = helper;
console.timeEnd('Initialize Helper');
console.time('Initialize Model');
params.Model = require('./models');
console.timeEnd('Initialize Model');
console.time('Initialize Middleware');
let middleware = require('./Middleware/middleware.js')(params);
params.Middleware = middleware;
app.use(middleware.setLanguage);
app.use(middleware.isBlacklistedIps);
app.use(middleware.requestLimiter);
app.use(middleware.flashMessage);
console.timeEnd('Initialize Middleware');
console.time('Initialize Logic');
let logicPath = './Logic';
requireFile(logicPath, './Logic/');
console.timeEnd('Initialize Logic');
console.time('Initialize Validator');
params.Validator = require('./Validator/Validator.js');
console.timeEnd('Initialize Validator');
console.time('Initialize Controller');
let controllerPath = './Controller';
requireFile(controllerPath, './Controller/');
console.timeEnd('Initialize Controller');
console.time('Initialize Routes');
let routesPath = './routes/route.js';
require(routesPath)(params);
console.timeEnd('Initialize Routes');
console.timeEnd('Initialize Core');
app.use((err, req, res, next) => {
console.log(err);
res.status(500).send({error: 'something blew up!'}).end();
});
app.get('/status', (req, res, next) => {
res.json({'status':'Server is running', 'date': Moment()});
});
// Catch 404 and forward to error handler
app.use((req, res, next) => {
let err = new Error('Not found');
err.status = 404;
err.title = 'Not Found';
next(err);
});
// Error handler, won't print stacktrace on production environment
app.use((err, req, res, next) => {
let error = (env.APP_ENV === 'production') ? {} : err;
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: error,
title: err.title || 'Error'
});
});
const APP_PORT = env.APP_PORT || 8432;
app.listen(APP_PORT, () => {
console.log(`Server running on port: ${APP_PORT}`);
});