forked from dekkerglen/CubeCobra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
286 lines (252 loc) · 7.25 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
// Load Environment Variables
require('dotenv').config();
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const session = require('express-session');
const passport = require('passport');
const http = require('http');
const fileUpload = require('express-fileupload');
const MongoDBStore = require('connect-mongodb-session')(session);
const winston = require('winston');
const WinstonCloudWatch = require('winston-cloudwatch');
const AWS = require('aws-sdk');
const onFinished = require('on-finished');
const uuid = require('uuid/v4');
const schedule = require('node-schedule');
const rateLimit = require('express-rate-limit');
const updatedb = require('./serverjs/updatecards.js');
const carddb = require('./serverjs/cards.js');
const CardRating = require('./models/cardrating');
const formatInfo = ({ message }) => {
try {
return JSON.stringify(message);
} catch (err) {
return 'Error formatting info';
}
};
const formatError = ({ message, stack, request }) =>
JSON.stringify({
level: 'error',
message,
target: request ? request.originalUrl : null,
uuid: request ? request.uuid : null,
stack: (stack || '').split('\n'),
});
const linearFormat = winston.format((info) => {
if (info.message.type === 'request') {
info.message = `request: ${info.message.path}`;
} else if (info.level === 'error') {
info.message = `${info.message} ${info.stack}`;
delete info.stack;
delete info.request;
}
delete info.type;
return info;
});
const consoleFormat = winston.format.combine(linearFormat(), winston.format.simple());
if (process.env.ENV === 'production') {
winston.configure({
level: 'info',
format: winston.format.json(),
exitOnError: false,
transports: [
new WinstonCloudWatch({
level: 'info',
cloudWatchLogs: new AWS.CloudWatchLogs(),
logGroupName: `${process.env.AWS_LOG_GROUP}_${process.env.AWS_LOG_STREAM}_info`,
logStreamName: uuid(),
awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
awsSecretKey: process.env.AWS_SECRET_ACCESS_KEY,
awsRegion: process.env.AWS_REGION,
retentionInDays: parseInt(process.env.LOG_RETENTION_DAYS, 10),
messageFormatter: formatInfo,
}),
new WinstonCloudWatch({
level: 'error',
cloudWatchLogs: new AWS.CloudWatchLogs(),
logGroupName: `${process.env.AWS_LOG_GROUP}_${process.env.AWS_LOG_STREAM}_error`,
logStreamName: uuid(),
awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
awsSecretKey: process.env.AWS_SECRET_ACCESS_KEY,
awsRegion: process.env.AWS_REGION,
retentionInDays: parseInt(process.env.LOG_RETENTION_DAYS, 10),
messageFormatter: formatError,
}),
],
});
} else {
winston.configure({
level: 'info',
format: winston.format.json(),
exitOnError: false,
transports: [new winston.transports.Console({ format: consoleFormat })],
});
}
// Connect db
mongoose.connect(process.env.MONGODB_URL, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.once('open', () => {
winston.info('Connected to Mongo.');
});
// Check for db errors
db.on('error', (err) => {
winston.error(err);
});
// Init app
const app = express();
const store = new MongoDBStore(
{
uri: process.env.MONGODB_URL,
collection: 'session_data',
},
(err) => {
if (err) {
winston.error('Store failed to connect to mongoDB.', { error: err });
}
},
);
// request timeout middleware
app.use((req, res, next) => {
req.setTimeout(60 * 1000, () => {
const err = new Error('Request Timeout');
err.status = 408;
next(err);
});
res.setTimeout(60 * 1000, () => {
const err = new Error('Service Unavailable');
err.status = 503;
next(err);
});
next();
});
// per-request logging configuration
app.use((req, res, next) => {
req.uuid = uuid();
req.logger = {
error: (err) => {
// err.requst = req;
winston.error({
message: err.message,
stack: err.stack,
request: req,
});
},
info: (message) => winston.info(message),
};
res.locals.requestId = req.uuid;
res.startTime = Date.now();
onFinished(res, (err, finalRes) => {
winston.info({
level: 'info',
type: 'request',
remoteAddr: req.ip,
requestId: req.uuid,
method: req.method,
path: req.path,
status: finalRes.statusCode,
length: finalRes.getHeader('content-length'),
elapsed: Date.now() - finalRes.startTime,
});
});
next();
});
// upload file middleware
app.use(fileUpload());
// Body parser middleware
app.use(
bodyParser.urlencoded({
limit: '200mb',
extended: true,
}),
);
app.use(
bodyParser.json({
limit: '200mb',
extended: true,
}),
);
// Load view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// Set Public Folder
app.use(express.static(path.join(__dirname, 'public')));
app.use('/js', express.static(path.join(__dirname, 'dist')));
app.use('/jquery-ui', express.static(`${__dirname}/node_modules/jquery-ui-dist/`));
const sessionOptions = {
secret: process.env.SESSION,
store,
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7, // 1 week
},
};
// Express session middleware
app.use(session(sessionOptions));
// Express messages middleware
app.use(require('connect-flash')());
app.use((req, res, next) => {
res.locals.messages = require('express-messages')(req, res);
res.locals.node_env = app.get('env');
next();
});
// Passport config and middleware
require('./config/passport')(passport);
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
res.locals.user = req.user || null;
next();
});
app.post('*', (req, res, next) => {
res.locals.user = req.user || null;
next();
});
// apply a rate limiter to the cube json endpoint
const apiLimiter = rateLimit({
windowMs: 60000,
max: 1,
message: '429: Too Many Requests',
});
app.use('/cube/api/cubeJSON', apiLimiter);
// Route files; they manage their own CSRF protection
const cubes = require('./routes/cube_routes');
const users = require('./routes/users_routes');
const devs = require('./routes/dev_routes');
const tools = require('./routes/tools_routes');
app.use('', require('./routes/root'));
app.use('/cube', cubes);
app.use('/user', users);
app.use('/dev', devs);
app.use('/tool', tools);
app.use((req, res) => {
res.status(404).render('misc/404', {});
});
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
req.logger.error(err);
if (!res.statusCode) {
res.status(500);
}
res.render('misc/500', {
error: err.message,
});
});
// scryfall updates this data at 9, so his will minimize staleness
schedule.scheduleJob('0 10 * * *', async () => {
winston.info('String midnight cardbase update...');
const ratings = await CardRating.find({}, 'name elo').lean();
updatedb.updateCardbase(ratings);
});
// Start server after carddb is initialized.
carddb.initializeCardDb().then(() => {
http.createServer(app).listen(process.env.PORT || 5000, '127.0.0.1', () => {
winston.info(`Server started on port ${process.env.PORT || 5000}...`);
});
});