forked from digitalbiblesociety/dbp-reader
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnextServer.js
More file actions
559 lines (494 loc) · 16.4 KB
/
nextServer.js
File metadata and controls
559 lines (494 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// nextServer.js
require('core-js');
require('regenerator-runtime');
const dotenv = require('dotenv');
const { ENV, getCacheTTL } = require('./app/utils/environmentConfig.js');
if (process.env.NODE_ENV !== ENV.PRODUCTION) {
dotenv.config();
}
const cp = require('child_process');
const express = require('express');
const next = require('next');
const compression = require('compression');
const fetch = require('axios');
const Busboy = require('busboy');
const port = process.env.PORT || 3000;
const dev = process.env.NODE_ENV === ENV.DEVELOPMENT;
const bugsnag = require('./app/utils/bugsnagServer');
const manifestJson = require('./public/manifest.json');
const checkBookId = require('./app/utils/checkBookName');
const isoOneToThree = require('./app/utils/isoOneToThree.json');
const { isM3U8Content } = require('./app/utils/parseM3U8.js');
const {
serverCachedFetch,
clearCache,
} = require('./app/utils/serverCachedFetch.js');
const { isEndpointNoCache } = require('./app/utils/apiEndpointConfig.js');
const app = next({ dev });
const handle = app.getRequestHandler();
async function renderAndCache(req, res, pagePath, queryParams) {
// Note: SSR route caching was disabled as it caused inconsistencies with audio types
// API caching is handled separately in serverCachedFetch.js
app.render(req, res, pagePath, queryParams);
}
app
.prepare()
.then(() => {
const server = express();
server.use(compression());
// Body parser middleware for JSON and URL-encoded requests
server.use(express.json());
server.use(express.urlencoded({ extended: true }));
// API Middleware - Injects API key server-side
// This prevents the API key from being exposed to the browser
server.use('/api', async (req, res) => {
try {
const { pathname, search } = new URL(
req.url,
`http://${req.headers.host}`,
);
// Extract the path after /api
const apiPath = pathname.replace(/^\/api/, '');
// Build the full API URL with the secret key (server-side only)
const apiKey = process.env.DBP_API_KEY;
const baseUrl = process.env.BASE_API_ROUTE;
if (!apiKey || !baseUrl) {
return res.status(fetch.HttpStatusCode.InternalServerError).json({
error: 'Server configuration error',
message: 'Missing API credentials',
});
}
// Clean up query parameters to avoid duplication
// Remove v=4 if it's already in the query string (browser shouldn't send it)
let cleanSearch = search;
if (cleanSearch) {
const params = new URLSearchParams(cleanSearch);
params.delete('v');
cleanSearch = params.toString();
cleanSearch = cleanSearch ? `?${cleanSearch}` : '';
}
// Append API key and version (v=4) server-side only
const separator = cleanSearch ? '&' : '?';
const fullUrl = `${baseUrl}${apiPath}${cleanSearch}${separator}key=${apiKey}&v=4`;
if (process.env.NODE_ENV === ENV.DEVELOPMENT) {
/* eslint-disable no-console */
console.log('[API Proxy] Forwarding to:', fullUrl);
/* eslint-enable no-console */
}
// Prepare axios config based on HTTP method
// Use normalized headers to ensure consistent API responses for both SSR and client-side requests
const axiosConfig = {
method: req.method.toLowerCase(),
url: fullUrl,
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Node.js)',
Accept: 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'keep-alive',
},
};
// Handle request body for POST, PUT, PATCH requests
if (['post', 'put', 'patch'].includes(req.method.toLowerCase())) {
const contentType = req.headers['content-type'] || '';
if (contentType.includes('application/json')) {
// Forward JSON body
axiosConfig.data = req.body;
axiosConfig.headers['Content-Type'] = 'application/json';
} else if (contentType.includes('application/x-www-form-urlencoded')) {
// Forward URL-encoded form data
axiosConfig.data = req.body;
axiosConfig.headers['Content-Type'] = contentType;
} else if (contentType.includes('multipart/form-data')) {
// Parse multipart form data to extract fields and files
const fields = {};
const files = {};
const busboy = Busboy({ headers: req.headers });
// Parse form fields
busboy.on('field', (fieldname, val) => {
fields[fieldname] = val;
});
// Parse file uploads
busboy.on('file', (fieldname, file, info) => {
files[fieldname] = { file, info };
});
// When parsing is complete, forward the request
await new Promise((resolve, reject) => {
busboy.on('close', () => {
// Reconstruct FormData with extracted fields
const FormData = require('form-data');
const formData = new FormData();
// Add all fields to the new FormData
Object.entries(fields).forEach(([key, value]) => {
formData.append(key, value);
});
// Add all files to the new FormData
Object.entries(files).forEach(([key, { file, info }]) => {
formData.append(key, file, info.filename);
});
axiosConfig.data = formData;
// Merge form-data headers with axios config headers
Object.assign(axiosConfig.headers, formData.getHeaders());
resolve();
});
busboy.on('error', reject);
req.pipe(busboy);
});
}
}
let useCache = req.method.toUpperCase() === 'GET';
endpointConfig = isEndpointNoCache(req.path, req.method);
if (endpointConfig && useCache && endpointConfig.cacheable === false) {
useCache = false;
}
const response = await serverCachedFetch(axiosConfig, useCache);
if (useCache) {
const cacheTime = Math.floor(getCacheTTL() / 1000); // Convert milliseconds to seconds for Cache-Control
res.setHeader('Cache-Control', `public, max-age=${cacheTime}`);
}
// Forward appropriate headers from the API response
const contentType = response.headers['content-type'] || 'application/json';
const contentDisposition = response.headers['content-disposition'] || '';
res.setHeader('Content-Type', contentType);
res.setHeader('Content-Disposition', contentDisposition);
// Check if this is M3U8 playlist content
if (isM3U8Content(contentType)) {
// Return M3U8 as plain text, properly decoding escaped newlines
let m3u8Content = response.data;
// Convert escaped newlines to actual newlines if needed
if (typeof m3u8Content === 'string') {
m3u8Content = m3u8Content.replace(/\\n/g, '\n');
}
res.status(response.status).send(m3u8Content);
} else {
// Return the response data as JSON
res.status(response.status).json(response.data);
}
} catch (error) {
/* eslint-disable no-console */
if (
process.env.APP_ENV === ENV.DEVELOPMENT ||
process.env.APP_ENV === ENV.NEWDATA
) {
console.error('[API Proxy Error]', error.message);
}
/* eslint-enable no-console */
// Return appropriate error status
const statusCode = error.response?.status || fetch.HttpStatusCode.InternalServerError;
const errorMessage =
error.response?.data?.message ||
error.message ||
'API request failed';
res.status(statusCode).json({
error: 'API proxy error',
message: errorMessage,
});
}
});
// TODO: Ask api team for the redirect for oauth be to /oauth instead of just /
// Then I can move all of the extra logic out of this route which is really gross
server.get('/', async (req, res) => {
let languageIso = 'eng';
let redirectPath = '/bible/EN1ESV/MAT/1';
if (req.headers['accept-language']) {
const languages = req.headers['accept-language'];
const langArray = languages
.split(',')
.map((lang) => {
const newLang = lang.includes('-')
? lang.split(';')[0].split('-')[0]
: lang.split(';')[0];
return isoOneToThree[newLang];
})
.filter((lang) => !!lang);
// Ensure languageIso is never undefined - default to 'eng' if parsing fails
languageIso = langArray[0] || 'eng';
}
if (languageIso && languageIso !== 'eng') {
const url = `${
process.env.BASE_API_ROUTE
}/bibles?language_code=${languageIso}&key=${
process.env.DBP_API_KEY
}&v=4&include_font=false`;
try {
const response = await serverCachedFetch({ url }, true);
// Get list of bibles that match language
const biblesInLanguage = response.data?.data;
// Check for first bible
if (biblesInLanguage && biblesInLanguage[0]) {
const bibleId = biblesInLanguage[0].abbr;
redirectPath = `/bible/${bibleId}/MAT/1`;
}
} catch (error) {
if (
process.env.APP_ENV === ENV.DEVELOPMENT ||
process.env.APP_ENV === ENV.NEWDATA
) {
/* eslint-disable no-console */
console.error('Error fetching bibles for language:', error.message);
/* eslint-enable no-console */
}
// Continue with default path on error
}
}
if (req.query.code) {
// Get encrypted string of user data
const encryptedData = req.query.code;
const userString = Buffer.from(encryptedData, 'base64').toString(
'ascii',
);
const userArray = userString.split(',');
res.redirect(
302,
`${redirectPath}?user_id=${userArray[0]}&user_email=${
userArray[1]
}&user_name=${userArray[2]}`,
);
} else {
res.redirect(302, redirectPath);
}
});
// Cache clearing endpoint - only available in development/staging environments
// In production, cache is cleared automatically via TTL (24 hours)
if (
process.env.APP_ENV === ENV.DEVELOPMENT ||
process.env.APP_ENV === ENV.NEWDATA
) {
server.get('/clean-the-cash', (req, res) => {
clearCache(); // Clear API request cache
res.send('Cleaned the cache');
});
}
server.get('/oauth', (req, res) => {
const userString = Buffer.from(req.query.code, 'base64').toString(
'ascii',
);
const userArray = userString.split(',');
res.redirect(
302,
`/bible/ENGESV/MAT/1?user_id=${userArray[0]}&user_email=${
userArray[1]
}&user_name=${userArray[2]}`,
);
});
const sitemapOptions = {
root: `${__dirname}/public/sitemaps/`,
headers: {
'Content-Type': 'text/xml;charset=UTF-8',
},
};
server.get('/sitemap.xml', (req, res) =>
res.status(fetch.HttpStatusCode.Ok).sendFile('sitemap-index.xml', sitemapOptions),
);
server.get('/robots.txt', (req, res) => {
res.set('Content-Type', 'text/plain');
res.status(fetch.HttpStatusCode.Ok).send(`User-agent: Googlebot
Disallow:
User-agent: Bingbot
Disallow:
User-agent: Slurp
Disallow:
User-agent: DuckDuckBot
Disallow:
User-agent: Baiduspider
Disallow:
User-agent: YandexBot
Disallow:
User-agent: Exabot
Disallow:
User-agent: facebot
Disallow:
User-agent: ia_archiver
Disallow:
User-agent: Sogou
Disallow:
User-agent: *
Disallow: /
`);
});
server.get('/git/version', async (req, res) => {
cp.exec('git rev-parse HEAD', (err, stdout) => {
if (err) {
res.status(fetch.HttpStatusCode.InternalServerError).send('Could not get the revision head');
} else {
res.status(fetch.HttpStatusCode.Ok).json({ head: stdout.replace('\n', '') });
}
});
});
server.get('/status', async (req, res) => {
const ok = await fetch
.get(`${process.env.BASE_API_ROUTE}/status`)
.then((r) => r.status >= 200 && r.status < 300)
.catch(() => false);
if (ok) {
res.sendStatus(fetch.HttpStatusCode.Ok);
} else {
res.sendStatus(fetch.HttpStatusCode.InternalServerError);
}
});
server.get('/manifest.json', (req, res) =>
res.status(fetch.HttpStatusCode.Ok).json(manifestJson),
);
server.get(/^\/dev-sitemap(.*)$/, (req, res) => {
const fileName = req.params[0]; // the “(.*)” capture
res.sendFile(fileName, sitemapOptions);
});
const faviconOptions = {
root: `${__dirname}/public/`,
};
server.get('/favicon.ico', (_, res) =>
res.status(fetch.HttpStatusCode.Ok).sendFile('favicon.ico', faviconOptions),
);
// Jesus Film Page
server.get('/jesus-film', (_, res) => {
res.redirect(fetch.HttpStatusCode.Found, '/jesus-film/eng');
});
server.get('/jesus-film/:iso', (req, res, nextP) => {
const actualPage = '/jesusFilm';
const iso = req.params.iso;
if (iso !== 'style.css' && !req.originalUrl.includes('/public')) {
renderAndCache(req, res, actualPage, { iso });
} else {
nextP();
}
});
// Static Information Pages
server.get('/terms', (req, res) => handle(req, res));
server.get('/privacy', (req, res) => handle(req, res));
server.get('/about', (req, res) => handle(req, res));
// Main App Page
server.get('/reset/password/:token', (req, res) => {
const actualPage = '/app';
const queryParams = {
token: req.params.token,
};
app.render(req, res, actualPage, queryParams);
});
server.get('/bible/:bibleId/:bookId/:chapter', (req, res, nextP) => {
const actualPage = '/app';
const bookId = checkBookId(req.params.bookId);
const chapter =
isNaN(parseInt(req.params.chapter, 10)) || !req.params.chapter
? '1'
: req.params.chapter;
const queryParams = {
bibleId: req.params.bibleId,
bookId,
chapter,
};
const userParams = {};
if (bookId !== req.params.bookId) {
res.redirect(302, `/bible/${req.params.bibleId}/${bookId}/${chapter}`);
} else if (
req.query.user_id &&
req.query.user_email &&
req.query.user_name
) {
userParams.userId = req.query.user_id;
userParams.userEmail = req.query.user_email;
userParams.userName = req.query.user_name;
// Remove all the query data so it doesn't appear in the url
req.query = {};
}
if (
queryParams.verse !== 'style.css' &&
!req.originalUrl.includes('/public') &&
!queryParams.verse
) {
renderAndCache(req, res, actualPage, { ...queryParams, ...userParams });
} else {
nextP();
}
});
server.get('/bible/:bibleId/:bookId/:chapter/:verse', (req, res, nextP) => {
const actualPage = '/app';
const bookId = checkBookId(req.params.bookId);
const chapter =
isNaN(parseInt(req.params.chapter, 10)) || !req.params.chapter
? '1'
: req.params.chapter;
const verse =
isNaN(parseInt(req.params.verse, 10)) || !req.params.verse
? '1'
: req.params.verse;
// Params may not actually be passed using this method
const queryParams = {
bibleId: req.params.bibleId,
bookId,
chapter,
verse,
};
if (bookId !== req.params.bookId) {
res.redirect(
302,
`/bible/${req.params.bibleId}/${bookId}/${chapter}/${verse}`,
);
} else if (
queryParams.verse !== 'style.css' &&
!req.originalUrl.includes('/public')
) {
renderAndCache(req, res, actualPage, queryParams);
} else {
nextP();
}
});
server.get('/bible/:bibleId/:bookId', (req, res, nextP) => {
const actualPage = '/app';
const bookId = checkBookId(req.params.bookId);
// Params may not actually be passed using this method
const queryParams = {
bibleId: req.params.bibleId,
bookId,
chapter: '1',
};
if (bookId !== req.params.bookId) {
res.redirect(302, `/bible/${req.params.bibleId}/${bookId}/1`);
} else if (
queryParams.verse !== 'style.css' &&
!req.originalUrl.includes('/public')
) {
renderAndCache(req, res, actualPage, queryParams);
} else {
nextP();
}
});
server.get('/bible/:bibleId', (req, res, nextP) => {
const actualPage = '/app';
// Params may not actually be passed using this method
const queryParams = {
bibleId: req.params.bibleId,
};
if (
queryParams.verse !== 'style.css' &&
!req.originalUrl.includes('/public')
) {
renderAndCache(req, res, actualPage, queryParams);
} else {
nextP();
}
});
server.get(/.*/, (req, res) => handle(req, res));
// catch-all for Next (including HMR, Fast Refresh, assets)
// everything under /_next
server.all(/^\/_next\/.*$/, (req, res) => handle(req, res));
// everything else (including “/”)
server.all(/^\/.*$/, (req, res) => handle(req, res));
server.listen(port, (err) => {
if (err && process.env.NODE_ENV === ENV.PRODUCTION) {
bugsnag.notify(err);
}
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`); // eslint-disable-line no-console
});
})
.catch((ex) => {
/* eslint-disable no-console */
console.error(
'------------------------^_^---*_*--$_$--------------------------------\n',
ex,
);
if (process.env.NODE_ENV === ENV.PRODUCTION) {
bugsnag.notify(ex);
}
/* eslint-enable no-console */
process.exit(1);
});