-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathexpress.js
More file actions
458 lines (394 loc) · 16.4 KB
/
express.js
File metadata and controls
458 lines (394 loc) · 16.4 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// ------------------------------------------------------------------------------
// express.js
// This file sets up and starts the Express server
// ------------------------------------------------------------------------------
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const logger = require('./helpers/logger');
const app = express();
let server = null; // Store server instance for graceful shutdown
let espnAthleteProvider = null; // Store ESPN athlete provider instance for cleanup
let hockeyTechProvider = null; // Store HockeyTech provider instance for cleanup
let mlbStatsProvider = null; // Store MLBStats provider instance for cleanup
module.exports = { init };
// ------------------------------------------------------------------------------
function init(port) {
logger.startup('Game Thumbs API - Starting Server');
// Trust proxy - required when running behind reverse proxy (nginx, load balancer, etc.)
// Set to number of proxy hops to trust (e.g., 2 for Cloudflare + Nginx)
// Set to 0 for local development (no proxies)
const trustProxyHops = parseInt(process.env.TRUST_PROXY || '2', 10);
app.set('trust proxy', trustProxyHops);
logger.info(`Trust proxy set to: ${trustProxyHops} hop(s)`);
const corsOptions = {
origin: process.env.CORS_ORIGIN || '*',
optionsSuccessStatus: 200,
credentials: false,
methods: ['GET', 'HEAD', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
exposedHeaders: ['Content-Type', 'Content-Length'],
maxAge: parseInt(process.env.CORS_MAX_AGE || '86400', 10),
};
app.use(cors(corsOptions));
app.use(helmet({
crossOriginResourcePolicy: { policy: "cross-origin" }
}));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Request-level timeout to prevent hanging requests
const OVERALL_REQUEST_TIMEOUT = parseInt(process.env.REQUEST_TIMEOUT || '10000', 10) * 2; // 2x the external request timeout
app.use((req, res, next) => {
// Set a timeout for the entire request
req.setTimeout(OVERALL_REQUEST_TIMEOUT, () => {
logger.error('Request timeout exceeded', {
URL: req.url,
IP: req.ip,
Timeout: `${OVERALL_REQUEST_TIMEOUT}ms`
});
if (!res.headersSent) {
res.status(408).json({ error: 'Request timeout' });
}
});
// Also set response timeout
res.setTimeout(OVERALL_REQUEST_TIMEOUT, () => {
logger.error('Response timeout exceeded', {
URL: req.url,
IP: req.ip,
Timeout: `${OVERALL_REQUEST_TIMEOUT}ms`
});
if (!res.headersSent) {
res.status(408).json({ error: 'Response timeout' });
}
});
next();
});
// Get real IP from proxy headers and log requests
app.use((req, res, next) => {
// Get real IP from proxy headers (nginx X-Real-IP)
const realIp = req.headers['x-real-ip'] || req.ip;
req.ip = realIp;
// Log the request (will be updated if cached)
req._startTime = Date.now();
next();
});
// Check cache first to determine if we need strict rate limiting
const { checkCacheMiddleware } = require('./helpers/imageCache');
app.use((req, res, next) => {
if (['thumb', 'logo', 'cover'].some(path => req.path.includes(path))) {
return checkCacheMiddleware(req, res, next);
}
next();
});
// Enable request-scoped log batching for image generation endpoints
app.use((req, res, next) => {
const isImageEndpoint = ['thumb', 'logo', 'cover', 'teamlogo', 'leaguelogo', 'leaguethumb', 'leaguecover'].some(path => req.path.includes(path));
if (isImageEndpoint) {
// Log incoming request immediately (not batched)
logger.requestStart(req);
const requestId = `${req.method}-${req.url}-${Date.now()}`;
const context = logger.startRequestBatching(requestId);
// Run the rest of the request in this context
return logger.runWithRequestContext(context, () => next());
} else {
next();
}
});
// Rate limiting configuration
const RATE_LIMIT_PER_MINUTE = parseInt(process.env.RATE_LIMIT_PER_MINUTE || '30', 10);
const RATE_LIMIT_ENABLED = RATE_LIMIT_PER_MINUTE > 0;
logger.info(`Rate limiting: ${RATE_LIMIT_ENABLED ? `enabled (${RATE_LIMIT_PER_MINUTE} requests/min)` : 'disabled'}`);
// Stricter limit for image generation endpoints
const imageGenerationLimiter = rateLimit({
windowMs: 60 * 1000,
max: RATE_LIMIT_PER_MINUTE,
message: { error: 'Too many image generation requests. Please try again later.' },
standardHeaders: true,
legacyHeaders: false,
skip: () => !RATE_LIMIT_ENABLED,
handler: (req, res) => {
logger.rate('Image generation blocked', {
IP: req.ip,
Method: req.method,
URL: req.url
});
res.status(429).json({ error: 'Too many image generation requests. Please try again later.' });
}
});
// General API rate limiter
const generalLimiter = rateLimit({
windowMs: 60 * 1000,
max: RATE_LIMIT_ENABLED ? RATE_LIMIT_PER_MINUTE * 3 : 0,
message: { error: 'Too many requests. Please try again later.' },
standardHeaders: true,
legacyHeaders: false,
skip: () => !RATE_LIMIT_ENABLED,
handler: (req, res) => {
logger.rate('API request blocked', {
IP: req.ip,
Method: req.method,
URL: req.url
});
res.status(429).json({ error: 'Too many requests. Please try again later.' });
}
});
// Apply rate limiting
app.use((req, res, next) => {
if (['thumb', 'logo', 'cover', 'teamlogo', 'leaguelogo', 'leaguethumb', 'leaguecover'].some(path => req.path.includes(path))) {
return imageGenerationLimiter(req, res, next);
}
return generalLimiter(req, res, next);
});
// Ignore browser icon requests early (before logging)
const ignoredPaths = ['/favicon.ico', '/apple-touch-icon.png', '/apple-touch-icon-precomposed.png'];
app.use((req, res, next) => {
if (ignoredPaths.includes(req.path)) {
return res.status(204).end();
}
next();
});
// Log all requests that make it past rate limiting and cache
app.use((req, res, next) => {
if (req.path !== '/health') {
res.on('finish', () => {
// Skip if already logged (e.g., by cache middleware)
if (!req._logged) {
const cached = !!req._servedFromRouteCache;
const isError = res.statusCode >= 400;
logger.request(req, cached, isError);
}
// Flush batched logs after request logging
logger.endRequestBatching();
});
}
next();
});
// Preload team overrides to show configuration at startup
require('./helpers/teamUtils');
// Health check endpoint (before loading other routes)
app.get('/health', (req, res) => {
res.status(200).json({
status: 'ok',
uptime: process.uptime(),
memory: process.memoryUsage(),
timestamp: new Date().toISOString()
});
});
const fs = require('fs');
const path = require('path');
const routesPath = path.join(__dirname, 'routes');
const APP_MODE = process.env.APP_MODE || 'standard';
if (APP_MODE === 'xcproxy') {
logger.info('XC Proxy mode - loading only xcproxy route');
// Auto-enable XC_PROXY when in xcproxy mode
process.env.XC_PROXY = 'true';
}
// Load route files and sort by priority (lower numbers first), then alphabetically
const routeFiles = fs.readdirSync(routesPath)
.filter(file => file.endsWith('.js'))
.filter(file => {
// In xcproxy mode, only load xcproxy.js
if (APP_MODE === 'xcproxy' && file !== 'xcproxy.js') {
return false;
}
return true;
})
.map(file => ({
file,
route: require(path.join(routesPath, file))
}))
.sort((a, b) => {
// Sort by priority first (lower numbers first, undefined = Infinity)
const priorityA = a.route.priority ?? Infinity;
const priorityB = b.route.priority ?? Infinity;
if (priorityA !== priorityB) {
return priorityA - priorityB;
}
// Then sort alphabetically by filename
return a.file.localeCompare(b.file);
});
// Register routes in sorted order
routeFiles.forEach(({ file, route }) => {
if (route.paths) {
for (const path of route.paths) {
registerRoute(path, route.handler, route.method);
logger.info(`Registered route: [${route.method.toUpperCase()}] ${path}${route.priority ? ` (priority: ${route.priority})` : ''}`);
}
}
else if (route.path) {
registerRoute(route.path, route.handler, route.method);
logger.info(`Registered route: [${route.method.toUpperCase()}] ${route.path}${route.priority ? ` (priority: ${route.priority})` : ''}`);
}
});
// Global error handler for uncaught route errors
app.use((err, req, res, next) => {
logger.error('Unhandled route error', {
Error: err.message,
URL: req.url,
IP: req.ip
}, err);
if (!res.headersSent) {
res.status(500).json({ error: 'Internal server error' });
}
});
// Catch-all handler for non-registered routes (must be last)
app.use((req, res) => {
logger.warn('Route not found', {
Method: req.method,
URL: req.url,
IP: req.ip
});
res.status(444).json({ error: 'Route not found' });
});
// Register default fonts (only when event overlays are enabled)
const { isEventOverlaysEnabled } = require('./helpers/featureFlags');
if (isEventOverlaysEnabled()) {
const { loadFont } = require('./helpers/fontRegistry');
loadFont('default_title.ttf', 'default_title');
loadFont('default_subtitle.ttf', 'default_subtitle');
}
server = app.listen(port, () => {
// Initialize provider caches in parallel (non-blocking)
Promise.all([
(async () => {
try {
espnAthleteProvider = require('./providers/ESPNAthleteProvider');
await espnAthleteProvider.initializeCache();
} catch (error) {
logger.error('Failed to initialize ESPN Athlete cache', { error: error.message });
}
})(),
(async () => {
try {
hockeyTechProvider = require('./providers/HockeyTechProvider');
await hockeyTechProvider.initializeCache();
} catch (error) {
logger.error('Failed to initialize HockeyTech config cache', { error: error.message });
}
})(),
(async () => {
try {
mlbStatsProvider = require('./providers/MLBStatsProvider');
await mlbStatsProvider.initializeCache();
} catch (error) {
logger.error('Failed to initialize MLBStats cache', { error: error.message });
}
})(),
(async () => {
try {
const espnProvider = require('./providers/ESPNProvider');
await espnProvider.initializeSportLeagueCache();
} catch (error) {
logger.error('Failed to initialize ESPN sport/league cache', { error: error.message });
}
})()
]).catch(err => {
logger.error('Unexpected error during provider initialization', { error: err.message });
});
logger.startup(`Server Running on Port ${port}`);
});
// Set server timeout to prevent hanging connections
const SERVER_TIMEOUT = parseInt(process.env.SERVER_TIMEOUT || '30000', 10);
server.timeout = SERVER_TIMEOUT;
server.keepAliveTimeout = SERVER_TIMEOUT;
server.headersTimeout = SERVER_TIMEOUT + 5000;
// Monitor active connections
let activeConnections = 0;
server.on('connection', (socket) => {
activeConnections++;
// Set socket timeout to prevent zombie connections
socket.setTimeout(SERVER_TIMEOUT);
socket.on('timeout', () => {
socket.destroy();
});
socket.on('close', () => {
activeConnections--;
});
socket.on('error', () => {
// Silent error handling - these are expected for aborted connections
});
});
// Log connection count every 5 minutes
setInterval(() => {
if (activeConnections > 0) {
logger.info(`Active connections: ${activeConnections}`);
}
}, 5 * 60 * 1000);
// Monitor memory usage every 10 minutes
setInterval(() => {
const memUsage = process.memoryUsage();
const memUsedMB = Math.round(memUsage.heapUsed / 1024 / 1024);
// Warning if memory usage is high
if (memUsedMB > 800) {
logger.warn(`High memory usage detected: ${memUsedMB}MB`);
}
}, 10 * 60 * 1000);
// Force garbage collection every hour if available
if (global.gc) {
setInterval(() => {
logger.info('Running manual garbage collection');
global.gc();
}, 60 * 60 * 1000);
}
// Graceful shutdown handlers
setupGracefulShutdown();
}
// ------------------------------------------------------------------------------
function registerRoute(path, handler, method = 'get') {
app[method](path, handler);
}
// ------------------------------------------------------------------------------
function setupGracefulShutdown() {
let isShuttingDown = false;
const shutdown = async (signal) => {
if (isShuttingDown) return;
isShuttingDown = true;
logger.info(`Received ${signal}, starting graceful shutdown...`);
// Stop ESPN Athlete provider refresh timers
if (espnAthleteProvider) {
espnAthleteProvider.stopAllRefreshes();
}
// Stop HockeyTech provider refresh timers
if (hockeyTechProvider) {
hockeyTechProvider.stopAllRefreshes();
}
// Stop accepting new connections
if (server) {
server.close(() => {
logger.info('Server closed, all connections ended');
process.exit(0);
});
// Force close after 10 seconds
setTimeout(() => {
logger.error('Forced shutdown after timeout');
process.exit(1);
}, 10000);
} else {
process.exit(0);
}
};
// Handle various termination signals
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
// Handle uncaught exceptions
process.on('uncaughtException', (err) => {
logger.error('Uncaught Exception', {
Error: err.message
}, err);
// Don't exit immediately, let the process continue
// but log it for debugging
});
// Handle unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
// Create an error object if reason is not already an error
const err = reason instanceof Error ? reason : new Error(String(reason));
logger.error('Unhandled Promise Rejection', {
Reason: String(reason),
Promise: String(promise)
}, err);
// Don't exit immediately, let the process continue
// but log it for debugging
});
}
// ------------------------------------------------------------------------------