forked from digitalbiblesociety/dbp-reader
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nextServer.js
379 lines (343 loc) · 10.7 KB
/
nextServer.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
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
// Has the test variable for if I run the project locally without newrelic
if (
(process.env.NODE_ENV === 'production' ||
process.env.NODE_ENV === 'staging') &&
process.env.TEST !== 'test'
) {
require('newrelic'); // eslint-disable-line
}
require('core-js');
require('regenerator-runtime');
const lscache = require('lscache');
const dotenv = require('dotenv');
if (process.env.NODE_ENV !== 'production') {
dotenv.config();
}
const cp = require('child_process');
const express = require('express');
const next = require('next');
const compression = require('compression');
const { LRUCache } = require('lru-cache');
const fetch = require('axios');
const port = process.env.PORT || 3000;
const dev = process.env.NODE_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 app = next({ dev });
const handle = app.getRequestHandler();
const ssrCache = new LRUCache({
max: 1000,
maxAge: dev ? 1000 * 60 * 5 : 1000 * 60 * 60 * 24,
});
async function renderAndCache(req, res, pagePath, queryParams) {
// Stop caching individual routes as it is causing inconsistencies with the audio types
app.render(req, res, pagePath, queryParams);
}
app
.prepare()
.then(() => {
const server = express();
server.use(compression());
// 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);
languageIso = langArray[0];
}
if (languageIso !== 'eng') {
const biblesData = await fetch.get(
`${
process.env.BASE_API_ROUTE
}/bibles?language_code=${languageIso}&key=${
process.env.DBP_API_KEY
}&v=4&include_font=false`,
)
.then((body) => body.data)
.catch((err) => {
if (process.env.NODE_ENV === 'development') {
/* eslint-disable no-console */
console.error(
'Error in get initial props bible for language: ',
err,
);
/* eslint-enable no-console */
}
return { data: [] };
});
// Get list of bibles that match language
const biblesInLanguage = biblesData.data;
// Check for first bible
if (biblesInLanguage[0]) {
const bibleId = biblesInLanguage[0].abbr;
redirectPath = `/bible/${bibleId}/MAT/1`;
}
}
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(
301,
`${redirectPath}?user_id=${userArray[0]}&user_email=${
userArray[1]
}&user_name=${userArray[2]}`,
);
} else {
res.redirect(301, `${redirectPath}`);
}
});
server.get('/clean-the-cash', (req, res) => {
ssrCache.clear();
lscache.flush();
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(
301,
`/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(200).sendFile('sitemap-index.xml', sitemapOptions),
);
server.get('/robots.txt', (req, res) => {
res.set('Content-Type', 'text/plain');
res.status(200).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(500).send('Could not get the revision head');
} else {
res.status(200).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(200);
} else {
res.sendStatus(500);
}
});
server.get('/manifest.json', (req, res) =>
res.status(200).json(manifestJson),
);
server.get('/dev-sitemap*', (req, res) =>
res.status(200).sendFile(req.originalUrl, sitemapOptions),
);
const faviconOptions = {
root: `${__dirname}/public/`,
};
server.get('/favicon.ico', (req, res) =>
res.status(200).sendFile('favicon.ico', faviconOptions),
);
// Jesus Film Page
server.get('/jesus-film', (req, res) => {
res.redirect(301, '/jesus-film/eng');
});
server.get('/jesus-film/:iso', (req, res) => {
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(301, `/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(
301,
`/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(301, `/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));
server.listen(port, (err) => {
if (
err &&
(process.env.NODE_ENV === 'production' ||
process.env.NODE_ENV === 'staging')
) {
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 !== 'development') {
bugsnag.notify(ex);
}
/* eslint-enable no-console */
process.exit(1);
});