-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
249 lines (194 loc) · 6.19 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
/** IMPORTS **/
const routes = require("./assets/data/routes.json");
const config = require("./assets/data/config.json");
const Color = require("./utils/Color");
const flash = require("connect-flash");
const sitemap = require("express-sitemap");
var Mongo = require("./utils/Mongo");
var Mailer = require("./utils/Mailer");
var PostOrm = require("./orms/PostOrm");
var UserOrm = require("./orms/UserOrm");
var FeedOrm = require("./orms/FeedOrm");
var Auth = require("./utils/Auth");
var Api = require("./utils/Api");
var RouteRender = require("./utils/RouteRender");
module.exports = class App {
/** SMALL CONSTRUCTORS **/
constructor(server, path){
this.server = server;
this.path = path;
App.server = server;
Mongo = new Mongo("blog", App);
Mailer = new Mailer(App);
PostOrm = new PostOrm(App);
UserOrm = new UserOrm(App);
FeedOrm = new FeedOrm(App);
Api = new Api(App);
RouteRender = new RouteRender(server, App);
Mongo.start();
}
/** REST **/
/**
* This is an instance of RouteRender
*/
static RouteRender(){
return RouteRender
}
/**
* This is an instance of PostOrm
*/
static PostOrm(){
return PostOrm
}
/**
* This is an instance of UserOrm
*/
static UserOrm(){
return UserOrm
}
static FeedOrm(){
return FeedOrm
}
/**
* This is an instance of Mongo
*/
static Mongo(){
return Mongo
}
/**
* This is an instance of Mail server
*/
static Mailer(){
return Mailer
}
/**
* This is an instance of Api
*/
static Api(){
return Api
}
/**
* This function replace all
* @param {String} data
* @param {String} charToReplace charToReplace
* @param {String} newChar newChar
* @return {String} data
*/
static replaceAll(data, charToReplace, newChar = ""){
if (App.isNull(data)) return data;
typeof data != "string" ? data = data.toString() : data = data;
while (data.includes(charToReplace))
data = data.replace(charToReplace, newChar);
return data;
}
/**
* This function debug data passed by parameter
* @param {*} data message
*/
static debug(data, type = "NORMAL"){
data instanceof Object ? data = JSON.stringify(data) : data = data;
const prefix = "[" + type + "]";
const prompt = " ⇒ ";
if (prefix.includes("ERROR")) console.log(Color.FgRed + prefix + Color.FgMagenta + prompt + Color.Reset + data);
else if (prefix.includes("ALERT")) console.log(Color.FgYellow + prefix + Color.FgMagenta + prompt + Color.Reset + data);
else if (prefix.includes("TEST")) console.log(Color.FgCyan + prefix + Color.FgMagenta + prompt + Color.Reset + data);
else console.log(Color.FgBlue + prefix + Color.FgMagenta + prompt + Color.Reset + data);
}
/**
* This function throw custom alerts
* @param {*} data alert
*/
static throwAlert(data){
data instanceof Object ? data = JSON.stringify(data) : data = data;
App.debug(data, "ALERT");
}
/**
* This function throw custom errors
* @param {*} err error
*/
static throwErr(err){
if(!App.isNull(err)) App.debug(err.message, "ERROR");
}
/**
* This function configure proxy server
* @param {*} rateLimit
*/
configureProxy(rateLimit){
this.server.enable("trust proxy");
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: "Too many accounts created from this IP, please try again after an hour"
});
this.server.use('/api/', apiLimiter);
}
/**
* This function configure the middlewares
* @param {*} cookieParser cookieParser
* @param {*} bodyParser bodyParser
* @param {*} session session
* @param {*} passport passport
*/
configureServer(cookieParser, bodyParser, session, passport){
this.server.use(cookieParser());
this.server.use(bodyParser.json());
this.server.use(bodyParser.urlencoded({extended: true}));
this.server.use(flash());
this.server.use(session(config.session));
this.server.use(passport.initialize());
this.server.use(passport.session());
passport.serializeUser((user, done) => done(null, user._id));
passport.deserializeUser((id, done) => App.UserOrm().getSchema().findById(id, (err, user) => done(err, user)));
Auth = new Auth(App, passport);
}
/**
* This function prepare node server
*/
prepareServer(){
this.server.set('views', this.path.join(__dirname, 'views'));
this.server.set('view engine', 'pug');
this.server.listen(config.port, () => {
App.debug("The server has been started! 🎨");
App.debug("The server listen port: " + config.port);
});
}
/**
* This function register a server routes
*/
prepareRoutes(){
RouteRender.renderPages(routes);
RouteRender.renderPosts();
RouteRender.renderAuth(Auth.Passport());
RouteRender.renderPanel();
RouteRender.renderApi();
RouteRender.renderSitemap();
RouteRender.renderErrors();
}
/**
* This function prepare the siteMap
*/
static prepareSitemap(){
const siteMap = sitemap({
url: config.url.includes("https://") ? config.url.replace("https://", "") : config.url.replace("http://", ""),
http: config.url.includes("https") ? "https" : "http"
});
siteMap.generate(App.server);
return siteMap;
}
/**
* This function check if data is null
* @param {*} data
* @return {boolean} isNull
*/
static isNull(data){
return data == null || data == undefined;
}
/**
* This function check if data is null
* @param {*} data
* @return {boolean} isNull
*/
isNull(data){
return data == null || data == undefined || data == "";
}
}