-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
453 lines (392 loc) · 15.6 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
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
import stream from 'stream';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { createServer } from 'http';
import { ExpressPeerServer } from 'peer';
import axios from 'axios';
import DateHelper from './lib/dateHelper.js';
import * as encodeHelper from './lib/encodeHelper.js';
import useAPIV1 from './server/api/v1/index.js'; // Main API
import useRoomAPI from './server/api/v1/rooms.js'; // Room-specific API
import { getFeaturedContent } from './server/services/featuredContent.js'; // Services
import roomRoute from './server/routes/rooms.js'; // Routes
import { handleRoomRequest } from './server/services/roomRequests.js';
import * as cache from './server/services/cache.js';
import * as jwtHelper from './server/services/jwt.js';
import localizationMiddleware from './server/services/localization.js';
import { startJobs } from './server/services/cron.js';
import * as google from './server/services/google.js';
import * as viewHelper from './server/utils/view.js'; // Utils
import * as mongo from './server/db/mongo.js'; // Database
startJobs();
const app = express();
const port = process.env.PORT || 3000;
const audioHost = 'https://ipod.dailypage.org';
const backendBaseUrl = `${(process.env.BACKEND_URL || `http://localhost:${port}`)}`;
const backendApiUrl = `${backendBaseUrl}/api/v1`;
const ROOM_BASED_CUTOFF = new Date('2024-12-31');
(async () => {
const dateParam = ':date([0-9]{4}-[0-9]{2}-[0-9]{2})';
try {
await mongo.initConnection();
google.init(mongo);
const whitelist = ['https://dailypage.org', 'http://localhost:3000'];
const corsOptions = {
origin: (origin, callback) => {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
};
app.use(cors(corsOptions));
app.use(localizationMiddleware);
app.options('*', cors(corsOptions));
if (process.env.NODE_ENV === 'production') {
app.use((req, res, next) => {
if (req.headers['x-forwarded-proto'] !== 'https') {
// res.redirect(`https://${req.headers.host}${req.path}`);
next();
} else {
next();
}
});
}
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set('views', './views');
app.set('view engine', 'pug');
useAPIV1(app, mongo);
useRoomAPI(app);
app.use('/', roomRoute);
const server = createServer(app)
const peerServer = ExpressPeerServer(server, {
debug: true,
});
app.use('/peerjs', peerServer);
server.listen(port, () => {
console.log(`Listening on port ${port}`);
});
app.get('/archive', async (_, res) => {
const combos = await cache.get('monthYearCombos', mongo.getPageMonthYearCombos);
res.render('archive', {
combos,
title: 'Archive',
});
});
app.get('/random', (_, res) => {
res.render('randomWriter', { title: 'Random Writer' });
});
app.get('/baseball', async (_, res) => {
const docTitles = await google.getDocTitles();
const sortable = [];
Object.keys(docTitles).forEach((slug) => sortable.push([slug, docTitles[slug].name]));
sortable.sort((a, b) => {
if (a[1] > b[1]) { return -1; }
if (b[1] > a[1]) { return 1; }
return 0;
});
const firstNumericElement = sortable.findIndex((el) => !Number.isNaN(parseInt(el[1], 10)));
const generalSortable = sortable.slice(0, firstNumericElement);
generalSortable.sort((a, b) => {
if (a[1] > b[1]) { return 1; }
if (b[1] > a[1]) { return -1; }
return 0;
});
const dateSortable = sortable.slice(firstNumericElement, sortable.length);
dateSortable.sort((a, b) => {
const date1 = Date.parse(a[1]);
const date2 = Date.parse(b[1]);
if (date1 > date2) { return -1; }
if (date2 > date1) { return 1; }
return 0;
});
const titlesWithHeaders = {
'General notes': generalSortable,
'Daily notes': dateSortable,
};
res.render('linkList', {
basePaths: '/baseball',
title: "Robert's Miscellaneous Baseball Notes",
titlesWithHeaders,
});
});
app.get('/baseball/:pageId', async (req, res) => {
res.send(await google.docText(req.params.pageId));
});
app.get('/iPod/:gen?', async (req, res) => {
res.render('iPod', {
backendURL: audioHost,
version: req.params.gen || '1g',
});
});
app.get('/music', async (req, res) => {
res.render('linkList', {
basePaths: ['/artist', '/album'],
title: 'Music',
titlesWithHeaders: { Artists: [], Albums: [] },
});
});
app.get('/music/meta/artist/:artistID', async (req, res) => {
const albumIDs = await google.getAlbumIDsByArtist(req.params.artistID);
const { Albums } = await cache.get('albums', google.getAlbums, [], 40 * 1000);
res.send(albumIDs.map((albumID) => Albums.find(
(el) => el[0] === albumID,
)));
});
app.get('/artist/:artistID', async (req, res) => {
const artistName = req.params.artistID;
let albums = JSON.parse((await axios.get(`${audioHost}/meta/music/artist/${artistName}/albums`)).body);
albums = albums.map((album) => [encodeHelper.htmlString(album), album]);
res.render('linkList', {
basePaths: `/artist/${artistName}/album`,
title: '← Music',
titleLink: '/music',
titlesWithHeaders: {
[artistName]: albums,
},
});
});
app.get('/album', async (req, res) => {
res.render('linkList', {
basePaths: '/album',
title: '← Music',
titleLink: '/music',
titlesWithHeaders: await cache.get('albums', google.getAlbums, [], 40 * 1000),
});
});
app.get('/artist', async (req, res) => {
let artistRes = JSON.parse((await axios.get(`${audioHost}/meta/music/artists`)).body);
artistRes = artistRes.map((artist) => [encodeHelper.htmlString(artist), artist]);
res.render('linkList', {
basePaths: '/artist',
title: '← Music',
titleLink: '/music',
titlesWithHeaders: { Artists: artistRes },
});
});
app.get('/music/meta/artist', async (req, res) => {
res.send(await cache.get('artists', google.getArtists, [], 40 * 1000));
});
app.get('/music/meta/album', async (req, res) => {
res.send(await cache.get('albums', google.getAlbums, [], 40 * 1000));
});
app.get('/music/meta/song', async (req, res) => {
res.send(await cache.get('songs', google.getSongs, [], 40 * 1000));
});
app.get('/music/meta/album/:albumID', async (req, res) => {
res.send({
albumArtist: await google.getArtist(req.params.albumID),
tracks: await cache.get(req.params.albumID, google.getTracks, [req.params.albumID],
2 * 60 * 1000),
});
});
app.get('/artist/:artistID/album/:albumID/:trackID', async (req, res) => {
const albumName = req.params.albumID;
const artist = req.params.artistID;
const trackList = JSON.parse((await axios.get(`${audioHost}/meta/music/artist/${artist}/${albumName}`)).body);
const { trackID } = req.params;
const trackPos = trackList.tracks.findIndex((el) => encodeHelper.htmlString(el.name) === encodeHelper.htmlString(trackID));
const trackArtist = trackList.tracks[trackPos].artist || trackList.albumArtist;
let nextTrackIndex = trackPos + 1;
if (trackPos === trackList.length - 1) {
nextTrackIndex = 0;
}
res.render('audioPlayer', {
host: audioHost,
title: trackList.tracks[trackPos].name,
artist: trackArtist,
albumArtist: trackList.albumArtist,
albumID: albumName,
albumName,
trackID: trackList.tracks[trackPos].id,
nextTrackIndex,
trackPos,
trackList,
});
});
app.get('/artist/:artistID/album/:albumID', async (req, res) => {
const artist = req.params.artistID;
const album = req.params.albumID;
const albumTracks = JSON.parse((await axios.get(`${audioHost}/meta/music/artist/${artist}/${album}`)).body);
res.redirect(`/artist/${artist}/album/${album}/${encodeHelper.htmlString(albumTracks.tracks[0].name)}`);
});
app.get('/audio/:fileID/:albumID*?.wav', async (req, res) => {
let readStream;
if (req.headers.range) {
const { range } = req.headers;
const parts = range.replace(/bytes=/, '').split('-');
const partialStart = parts[0];
const partialEnd = parts[1];
const start = parseInt(partialStart, 10);
let end;
const total = parseInt((await cache.get(req.params.albumID, google.getTracks, [
req.params.albumID], 30 * 1000)).find((el) => el[0] === req.params.fileID)[2], 10);
let buffer;
const chunksize = 1125000;
if (partialEnd && partialEnd !== '1') {
end = parseInt(partialEnd, 10);
if (end - start > chunksize && end - start !== total - 1) {
end = start + chunksize - 1;
}
buffer = await google.wavFromText(req.params.fileID, req.params.albumID, start, end);
} else {
end = start + chunksize - 1;
buffer = await google.wavFromText(req.params.fileID, req.params.albumID, start, end);
if (buffer.byteLength > 1 && partialEnd === '1') {
end = 1;
buffer = Buffer.from(buffer.toString('base64', 0, 1), 'base64');
}
}
readStream = new stream.PassThrough();
readStream.end(buffer);
res.writeHead(206, {
Range: `bytes ${start}-${end}/${total}`,
'Content-Range': `bytes ${start}-${end}/${total}`,
'Content-Length': buffer.byteLength,
'Accept-Ranges': 'bytes',
'Content-Type': 'audio/x-wav',
});
readStream.pipe(res);
} else {
const buffer = await cache.get(req.params.fileID, google.wavFromText, [req.params.fileID,
req.params.albumID], 5 * 60 * 1000);
const total = buffer.byteLength;
readStream = new stream.PassThrough();
readStream.end(buffer);
res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'audio/x-wav' });
readStream.pipe(res);
}
});
app.get('/today', (_, res) => {
res.redirect(`/rooms/overview/${DateHelper.currentDate()}`);
});
app.get('/:year([0-9]{4})/:month(1[0-2]|(0?[1-9]))', async (req, res) => {
const { year, month } = req.params;
const formattedTime = `${DateHelper.monthName(month)} ${year}`;
const dates = await cache.get(`${year}-${month}`, mongo.getPageDatesByYearAndMonth, [year, month]);
res.render('yearMonth', {
title: `Daily Pages for ${formattedTime}`,
header: formattedTime,
dates,
});
});
app.get(`/rooms/overview/${dateParam}`, async (req, res) => {
const requestedDate = new Date(req.params.date);
try {
if (requestedDate < ROOM_BASED_CUTOFF) {
// Legacy Concatenated View
const pageData = await cache.get(req.params.date, mongo.getPage, [req.params.date]);
const [errorMessage, text] = viewHelper.archiveContent(pageData);
return res.render('archivedPage', {
title: `Daily Page for ${DateHelper.formatDate(req.params.date, 'long')}`,
header: DateHelper.formatDate(req.params.date, 'long'),
errorMessage,
text,
});
} else {
// Room-Based View
const pages = await mongo.pagesByDate(req.params.date); // Fetch all pages for the date
const rooms = await mongo.getAllRooms(); // Fetch all room metadata
const roomContents = rooms.map((room) => {
const page = pages.find((p) => p.room === room._id); // Find the page matching the room
const [errorMessage, text] = viewHelper.archiveContent(page)
return {
name: room.name,
id: room._id,
content: page ? text : null, // Include content or null if no page exists
};
});
res.render('archivedPage', {
title: `Daily Page for ${DateHelper.formatDate(req.params.date, 'long')}`,
header: DateHelper.formatDate(req.params.date, 'long'),
date: req.params.date,
roomContents,
});
}
} catch (error) {
console.error('Error fetching archive content:', error.message);
res.render('archivedPage', {
title: 'Error',
header: 'An Error Occurred',
errorMessage: 'An error occurred while loading the archive. Please try again later.',
});
}
});
app.get(`/rooms/:room([a-zA-Z0-9\-]+)/${dateParam}`, async (req, res) => {
const roomReq = req.params.room;
let roomMetdata = await mongo.getRoomMetadata(roomReq);
const dateAndRoom = `${DateHelper.formatDate(req.params.date, 'long')} - ${roomMetdata.name} Room`;
const pageData = await cache.get(req.params.date, mongo.getPage,
[req.params.date, roomReq, req.query]);
const [errorMessage, text] = viewHelper.archiveContent(pageData);
res.render('archivedPage', {
title: `Daily Page for ${dateAndRoom}`,
header: dateAndRoom,
errorMessage,
text,
});
});
app.get('/support', (_, res) => res.render('support', { title: 'Daily Page - Support' }));
app.post('/request-room', handleRoomRequest);
app.get('/rooms/:room_id', async (req, res) => {
const { room_id } = req.params;
try {
// Fetch room metadata
const roomMetadata = await mongo.getRoomMetadata(room_id);
if (!roomMetadata) {
return res.status(404).render('error', { message: 'Room not found.' });
}
// Fetch peer IDs for the room
const peerIDs = await mongo.peerIDs(room_id);
if (peerIDs.length >= 6) {
return res.render('fullRoom', {
room: viewHelper.capitalize(roomMetadata.name),
description: roomMetadata.description,
});
}
// Determine the targetPeerId (random existing peer if available, or default to 0)
const targetPeerId = peerIDs.length > 0
? peerIDs[Math.floor(Math.random() * peerIDs.length)]
: '0';
// Render the editor for the room
const date = DateHelper.currentDate('long');
res.render('index', {
title: `Daily Page - ${roomMetadata.name}`,
description: roomMetadata.description,
date,
room: roomMetadata._id,
header: `${date} - ${viewHelper.capitalize(roomMetadata.name)} Room`,
backendURL: backendApiUrl,
sessionID: jwtHelper.expiringKey(),
targetPeerId,
});
} catch (error) {
console.error('Error loading room editor:', error.message);
res.status(500).render('error', { message: 'An error occurred while loading the room.' });
}
});
app.get('/', async (req, res) => {
const featuredContent = await getFeaturedContent();
res.render('about', {
title: res.locals.translations.title,
translations: res.locals.translations,
lang: res.locals.lang,
featuredContent,
});
});
app.get('*', (req, res) => {
res.status(404).render('404', {
title: 'Page Not Found',
translations: res.locals.translations,
lang: res.locals.lang,
});
});
} catch (error) {
console.log(`Server startup failed: ${error.message}`); // eslint-disable-line no-console
console.log(error.stack);
}
})();