This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseFunctions.js
241 lines (204 loc) · 5.46 KB
/
useFunctions.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
var colors = require("colors");
var functions = module.exports = {};
//View engine locals variables
functions.userViewVars = function(req, res, next){
if(req.user)
res.locals.user = req.user;
else if(req.app.isDev == true){
req.user = {name: '[Dev]', subteam: 'support'};
res.locals.user = req.user;
}
next();
}
/**
* Gets event info from local db
* @param {*} req
* @param {*} res
* @param {*} next
*/
functions.getEventInfo = function(req, res, next) {
//req.passport = passport;
req.event = {
key: "undefined",
name: "undefined"
};
//Get s collections for finding current event
var db = req.db;
var current = db.get('current');
var events = db.get('events');
//finds current event
current.find({}, {}, function(e, current) {
//sets locals to no event defined just in case we don't find thing and we can just do next();
var eventId = 'No event defined';
var eventYear = 'No year defined';
res.locals.eventName = eventId;
//if exist
if (current && current[0]){
eventId = current[0].event;
eventYear = parseInt(eventId)
//set event key
req.event.key = eventId;
req.event.year = eventYear;
res.locals.event_key = req.event.key;
res.locals.event_year = req.event.year;
//find data for current event
events.find({ key: eventId }, {}, function(e, event){
if(e){
console.error(e);
return next();
}
//set tournament thing to event name
if(event && event[0]){
res.locals.eventName = event[0].name;
req.event.name = event[0].name;
next();
}else{
next();
}
});
}else{
next();
}
});
}
/**
* logger for res.log
* @param {*} req
* @param {*} res
* @param {*} next
*/
functions.logger = function(req, res, next){
res.log = function(message, param2, param3){
var color, override = false;
if(typeof(param2) == "boolean")
override = param2;
if(typeof(param2) == "string")
color = param2;
if(typeof(param3) == "boolean")
override = param3;
if(typeof(param3) == "string")
color = param3;
//res.debug is set to app.debug inside app.js
if(req.app.debug || override){
if(typeof(message) == "string" && color != undefined)
console.log(message[color]);
else
console.log(message);
}
}
//Sets variables accessible to any page from req (request) object
//req.requestTime = Date.now(); req.requestTime IS NOW SET INSIDE APP.JS
//formatted request time for logging
let d = new Date(req.requestTime),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear(),
hours = d.getHours(),
minutes = d.getMinutes(),
seconds = d.getSeconds();
month = month.length<2? '0'+month : month;
day = day.length<2? '0'+day : day;
let formattedReqTime = (
[year, month, day, [hours, minutes, seconds].join(':')].join('-')
)
//user agent
req.shortagent = {
ip: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
device: req.useragent.isMobile ? "mobile" : req.useragent.isDesktop ? "desktop" : (req.useragent.isiPad || req.useragent.isAndroidTablet) ? "tablet" : req.useragent.isBot ? "bot" : "other",
os: req.useragent.os,
browser: req.useragent.browser
}
//logs request
console.log( (req.method).red
+ " Request from "
+ req.shortagent.ip
+ " on "
+ req.shortagent.device
+ "|"
+ req.shortagent.os
+ "|"
+ req.shortagent.browser
+ " to "
+ (req.url).cyan
+ " at "
+ formattedReqTime);
next();
}
/**
* Logs when res.render is called
* @param {*} req
* @param {*} res
* @param {*} next
*/
functions.renderLogger = function(req, res, next){
//Changes res.render
res.render = (function(link, param){
var cached_function = res.render;
return function(link, param){
//stores pre-render post-request time
let beforeRenderTime = Date.now() - req.requestTime;
//applies render function
let result = cached_function.apply(this, arguments);
//stores post-render time
let renderTime = Date.now() - req.requestTime - beforeRenderTime;
//logs if debug
this.log("Completed route in "
+ (beforeRenderTime).toString().yellow
+ " ms; Rendered page in "
+ (renderTime).toString().yellow
+ " ms");
return result;
}
}());
next();
}
/**
* Sets up node rest client
* @param {*} req
* @param {*} res
* @param {*} next
*/
functions.setupNodeRestClient = function(req, res, next){
var passwordsCol = req.db.get("passwords");
//Get thebluealliance API key from db
passwordsCol.find({ name:"thebluealliance-args" }, function(e, args){
if(e || !args[0]){
res.log(e, true);
return res.status(500).send("couldn't find TBA args in db");
}
args = args[0];
//set up node rest client
var Client = require('node-rest-client').Client;
var client = new Client();
//adds client func and args to req
req.client = client;
req.tbaRequestArgs = args;
next();
});
};
/**
* Handles 404 errors
* @param {*} req
* @param {*} res
* @param {*} next
*/
functions.notFoundHandler = function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
};
/**
* Handles other errors
* @param {} err
* @param {*} req
* @param {*} res
* @param {*} next
*/
functions.errorHandler = function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
}