forked from daniloc/airtable-api-proxy
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
475 lines (404 loc) · 18 KB
/
server.js
File metadata and controls
475 lines (404 loc) · 18 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
/**
* @file Express server with IP validation middleware and graceful cleanup
* @author Avana Vana <avana@esovdb.org>
* @version 4.0.0
*/
const dotenv = require('dotenv').config();
const express = require('express');
const cleanUp = require('node-cleanup');
const cors = require('cors');
const { db, monitor } = require('./batch');
const { appReady, patternsToRegEx } = require('./util');
const esovdb = require('./esovdb');
const webhooks = require('./webhooks');
const youtube = require('./youtube');
const zotero = require('./zotero');
const app = express();
const port = Number(process.env.PORT || 3000);
const middleware = {
/**
* Middleware for blacklisting or whitelisting IP addresses and/or IP address ranges, which can be passed to specific endpoints
*
* @method validateReq
* @requires util.patternsToRegEx
* @param {!express:Request} req - Express.js HTTP request context, an enhanced version of Node's http.IncomingMessage class
* @param {!express:Response} res - Express.js HTTP response context, an enhanced version of Node's http.ServerResponse class
* @param {!express:NextFunction} next - The next middleware function in the stack
*/
validateReq: (req, res, next) => {
const d = new Date(), ip = (req.headers['x-forwarded-for'] || req.connection.remoteAddress || '').split(',')[0].trim();
if (patternsToRegEx(process.env.IP_BLACKLIST).test(ip)) {
const err = {
Error: 'Access denied.',
};
console.error(`[${d.toLocaleString()}] (${ip})\n${err.Error}`);
res.status(401).send(JSON.stringify(err));
} else {
console.log(`[${d.toLocaleString()}] (${ip})\nAccess granted.`);
next();
}
},
auth: (req, res, next) => {
if (req.headers['x-esovdb-key'] && req.headers['x-esovdb-key'] === process.env.ESOVDB_KEY) {
console.log('ESOVDB key validated.');
next();
} else if (req.headers['esovdb-key'] && req.headers['esovdb-key'] === process.env.ESOVDB_KEY) {
console.log('ESOVDB key validated (using deprecated header—update this client!');
next();
} else if (req.headers['X-RapidAPI-Proxy-Secret'] && req.headers['X-RapidAPI-Proxy-Secret'] === process.env.RAPIDAPI_SECRET) {
console.log('RapidAPI proxy secret validated.');
next();
} else {
const d = new Date(), ip = (req.headers['x-forwarded-for'] || req.connection.remoteAddress || '').split(',')[0].trim();
console.error(`[${d.toLocaleString()}] (${ip}) Unauthorized attempted access of ${req.path} without a valid ESOVDB key.`);
res.status(401).send('Unauthorized access. Visit https://rapidapi.com/the-earth-science-online-video-database-the-earth-science-online-video-database-default/api/the-earth-science-online-video-database for access.');
}
},
allowCORS: (req, res, next) => {
res.set('Access-Control-Allow-Origin', '*');
next();
}
}
/**
* API endpoint for querying the entire ESOVDB—returns JSON. Used with the premium header 'esovdb-no-cache', always returns fresh results.
* @requires esovdb
* @callback esovdb.getLatest
*/
app.get('/v1/videos', [ middleware.auth, middleware.validateReq ], (req, res) => {
esovdb.getLatest(req, res);
});
/**
* API endpoint for querying the ESOVDB—returns JSON. All request params and request query params documented in [esovdb.queryVideos]{@link esovdb.queryVideos}.
* @requires esovdb
* @callback esovdb.queryVideos
*/
app.get('/v1/videos/query/:pg?', [ middleware.auth, middleware.validateReq ], (req, res) => {
esovdb.queryVideos(req, res);
});
/**
* API endpoint for querying the ESOVDB (Videos table) for a single YouTube video—returns simplified JSON. All request params and request query params documented in [esovdb.queryYouTubeVideos]{@link esovdb.queryYouTubeVideos}.
* @requires esovdb
* @callback esovdb.queryYouTubeVideos
*/
app.get('/v1/videos/youtube/:id?', [ middleware.validateReq, middleware.allowCORS ], (req, res) => {
esovdb.queryYouTubeVideos(req, res);
});
/**
* API endpoint for querying the ESOVDB (Videos & Submissions tables) for a single YouTube video or submission (if both are found, video takes precedence)—returns simplified JSON. All request params and request query params documented in [esovdb.queryYouTubeVideos]{@link esovdb.queryYouTubeVideos}.
* @requires esovdb
* @callback esovdb.queryYouTubeVideosAndSubmissions
*/
app.get('/v1/videos-submissions/youtube/:id?', [ middleware.validateReq, middleware.allowCORS ], (req, res) => {
esovdb.queryYouTubeVideosAndSubmissions(req, res);
});
/**
* API endpoint for selecting a single video from the ESOVDB by its ESOVDB Airtable ID—returns JSON. With the premium header 'esovdb-no-cache' this endpoint always returns fresh results.
* @requires esovdb
* @callback esovdb.getVideoById
*/
app.get('/v1/videos/:id', [ middleware.auth, middleware.validateReq ], (req, res) => {
esovdb.getVideoById(req, res);
});
/**
* API endpoint for back-syncing Zotero data with the ESOVDB after adding or updating items on Zotero.
* @requires esovdb
* @callback esovdb.updateVideos
*/
app.post('/:table/update', [ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ], (req, res) => {
esovdb.updateTable(req, res);
});
/**
* Combined API endpoints for ESOVDB POST (onCreateRecord), PUT (onUpdateRecord), OPTIONS (CORS pre-flight), and DELETE (onDeleteRecord) automations
* @requires zotero
* @callback zotero.sync
*/
app.route('/zotero/:kind')
.post([ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ], (req, res) => {
console.log('Performing zotero/${req.params.kind}/create API request...');
zotero.sync(req, res);
})
.put([ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ], (req, res) => {
console.log('Performing zotero/${req.params.kind}/update API request...');
zotero.sync(req, res);
})
.options(cors())
.delete([ middleware.auth, middleware.validateReq, cors(), express.urlencoded({ extended: true }), express.json() ], (req, res) => {
console.log('Performing zotero/${req.params.kind}/delete API request...');
zotero.sync(req, res);
});
/**
* API POST endpoint for broadcasting batches of new submissions from the ESOVDB YouTube Watchlist Github Actions Workflow
* @requires webhooks
* @callback webhooks.execute
*/
app.post('/webhooks/discord/watchlist-submission-total', [ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ], async (req, res) => {
console.log('Performing webhooks/discord/newWatchlistSubmissionTotal API request...');
const response = await webhooks.execute(req.body, 'discord', 'newWatchlistSubmissionTotal');
if (!response || response.status >= 400) throw new Error('[ERROR] Unable to send Discord watchlist submission total webhook.');
res.status(200).send(response.config && response.config.data ? response.config.data : 'OK');
});
/**
* API POST endpoint for handling new submissions from the ESOVDB Discord #submissions channel
* @requires webhooks
* @callback webhooks.execute
*/
app.post('/webhooks/discord', [ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ], async (req, res) => {
console.log('Performing webhooks/discord/userSubmission API request...');
const response = await webhooks.execute(req.body, 'discord', 'userSubmission');
if (!response || response.status >= 400) throw new Error('[ERROR] Unable to respond to Discord user submission.');
res.status(200).send(response.config && response.config.data ? response.config.data : 'OK');
});
/**
* Combined API endpoints sfor handling new submissions sent to the ESOVDB Twitter account, @esovdb with a hashtag of #submit, as well as Twitter's webhook verification
* @requires webhooks
* @callback webhooks.execute
*/
app.route('/webhooks/twitter')
.all([ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ], (req, res, next) => { next(); })
.post(async (req, res) => {
console.log('Performing webhooks/twitter API request...');
const response = await webhooks.execute(req.body, 'twitter', '{event.type}');
if (!response || response.status >= 400) throw new Error('[ERROR] Unable to respond to Twitter webhook event.');
res.status(200).send(response.config && response.config.data ? response.config.data : 'OK');
})
.get((req, res) => {
res.status(200).send('OK (Placeholder)');
});
/**
* Combined API endpoints for managing ESOVDB webhook subscriptions
* @requires webhooks
* @callback webhooks.list, webhooks.manage
*/
app.route('/webhooks')
.get([ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ], (req, res) => {
console.log('Performing webhooks/list API request...');
webhooks.list(req, res);
})
.post([ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ], (req, res) => {
console.log('Performing webhooks/create API request...');
webhooks.manage(req, res);
})
.put([ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ], (req, res) => {
console.log('Performing webhooks/update API request...');
webhooks.manage(req, res);
})
.options(cors())
.delete([ middleware.auth, middleware.validateReq, cors(), express.urlencoded({ extended: true }), express.json() ], (req, res) => {
console.log('Performing webhooks/delete API request...');
webhooks.manage(req, res);
});
/**
* API endpoint for submitting a YouTube channel's videos to the ESOVDB
* @requires youtube
* @callback - youtube.getChannelVideos
*/
app.post('/submissions/youtube/channel', [ middleware.auth, middleware.validateReq, cors(), express.urlencoded({ extended: true }), express.json() ], (req, res) => {
console.log('Performing submissions/youtube channel API request...');
youtube.getChannelVideos(req, res);
});
/**
* API endpoint for submitting a YouTube playlist's videos to the ESOVDB
* @requires youtube
* @callback - youtube.getPlaylistVideos
*/
app.post('/submissions/youtube/playlist', [ middleware.auth, middleware.validateReq, cors(), express.urlencoded({ extended: true }), express.json() ], (req, res) => {
console.log('Performing submissions/youtube playlist API request...');
youtube.getPlaylistVideos(req, res);
});
/**
* API endpoint for submitting a single YouTube video to the ESOVDB (e.g. via "Is Video on ESOVDB?" iOS shorcut)
* @requires esovdb
* @callback - esovdb.newVideoSubmission
*/
app.post('/submissions/youtube/video/:id', [ middleware.auth, middleware.validateReq, cors(), express.urlencoded({ extended: true }), express.json() ], (req, res) => {
console.log('Performing submissions/youtube single video API request...');
esovdb.newVideoSubmission(req, res);
});
app.get('/health', async (req, res) => {
res.status(200).json({ ok: true });
// try {
// await db.connect();
// res.status(200).json({ ok: true });
// } catch (err) {
// res.status(503).json({
// ok: false,
// error: err instanceof Error ? err.message : String(err),
// });
// }
});
/**
* API endpoint for querying the entire ESOVDB submissions table—returns JSON. Used with the premium header 'esovdb-no-cache', always returns fresh results.
* @requires esovdb
* @callback esovdb.getLatestSubmissions
*/
app.get('/v1/submissions', [ middleware.auth, middleware.validateReq ], (req, res) => {
esovdb.getLatestSubmissions(req, res);
});
/**
* API endpoint for querying the ESOVDB submissions table—returns JSON. All request params and request query params documented in [esovdb.querySubmissions]{@link esovdb.querySubmissions}.
* @requires esovdb
* @callback esovdb.querySubmissions
*/
app.get('/v1/submissions/query/:pg?', [ middleware.auth, middleware.validateReq ], (req, res) => {
esovdb.querySubmissions(req, res);
});
/**
* API endpoint for querying the ESOVDB (both Videos and Submissions tables) for a single YouTube video—returns simplified JSON. All request params and request query params documented in [esovdb.queryYouTubeVideosAndSubmissions]{@link esovdb.queryYouTubeVideosAndSubmissions}.
* @requires esovdb
* @callback esovdb.queryYouTubeVideosAndSubmissions
*/
app.get('/v1/submissions/youtube/video/:id?', [ middleware.validateReq, middleware.allowCORS ], (req, res) => {
esovdb.queryYouTubeVideosAndSubmissions(req, res);
});
/**
* Combined API endpoints for managing the ESOVDB watchlist
* @requires esovdb
* @callback esovdb.watchlist.list, esovdb.watchlist.add, esovdb.watchlist.update, esovdb.watchlist.remove
*/
app.route('/watch')
.get(
[ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ],
async (req, res) => {
console.log('Performing watch/list API request...');
try {
const includeInactive = req.query && typeof req.query.includeInactive !== 'undefined' && String(req.query.includeInactive).toLowerCase() === 'true';
const records = await esovdb.watchlist.list({ includeInactive });
res.status(200).send(records);
} catch (err) {
console.error('[ERROR] watch/list:', err);
res.status(500).send(String(err && err.message ? err.message : err));
}
}
)
.post(
[ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ],
async (req, res) => {
console.log('Performing watch/add API request...');
try {
const body = req.body || {};
const created = await esovdb.watchlist.add(body);
res.status(201).send({
ok: true,
code: 'WATCHLIST_CREATED',
message: `Successfully added YouTube ${created.fields.Type === 'Channel' ? 'channel' : 'playlist'} "${created.fields.Name}" to the ESOVDB watchlist.`,
record: created
});
} catch (err) {
const message = String(err && err.message ? err.message : err);
console.error('[ERROR] watch/add:', err);
if (err && err.code === 'WATCHLIST_DUPLICATE') {
return res.status(409).send({
ok: false,
code: err.code,
message
});
}
res.status(500).send({
ok: false,
code: 'WATCHLIST_ADD_FAILED',
message
});
}
}
)
.patch(
[ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ],
async (req, res) => {
console.log('Performing watch/update API request...');
try {
const body = req.body || {};
const sourceId = body.sourceId;
const checkUpdatedItem = Boolean(body.checkUpdatedItem);
if (!sourceId) return res.status(400).send('Missing required field "sourceId" in request body.');
const fields = Object.assign({}, body);
delete fields.sourceId;
delete fields.checkUpdatedItem;
const updated = await esovdb.watchlist.update(
{ sourceId: sourceId },
fields,
{ checkUpdatedItem: checkUpdatedItem }
);
res.status(200).send(updated);
} catch (err) {
console.error('[ERROR] watch/update:', err);
res.status(500).send(String(err && err.message ? err.message : err));
}
}
)
.options(cors())
.delete(
[ middleware.auth, middleware.validateReq, cors(), express.urlencoded({ extended: true }), express.json() ],
async (req, res) => {
console.log('Performing watch/delete API request...');
try {
const body = req.body || {};
const sourceId = body.sourceId;
if (!sourceId) return res.status(400).send('Missing required field "sourceId" in request body.');
await esovdb.watchlist.remove({ sourceId: sourceId });
res.status(204).end();
} catch (err) {
console.error('[ERROR] watch/delete:', err);
res.status(500).send(String(err && err.message ? err.message : err));
}
}
);
/**
* API endpoint for retrieving a single watchlist item from the ESOVDB
* @requires esovdb
* @callback esovdb.watchlist.getBySourceId
*/
app.route('/watch/:sourceId')
.get(
[ middleware.auth, middleware.validateReq, express.urlencoded({ extended: true }), express.json() ],
async (req, res) => {
console.log('Performing watch/get API request...');
try {
const sourceId = req.params.sourceId;
const record = await esovdb.watchlist.getBySourceId(sourceId);
res.status(200).send(record);
} catch (err) {
console.error('[ERROR] watch/get:', err);
res.status(404).send(String(err && err.message ? err.message : err));
}
}
)
.options(cors());
/**
* API endpoint which is the end of all other endpoints
* @callback - Sends an HTTP 400 Bad Request status code and an error message in JSON format
*/
app.get('/*', (req, res) => {
const err = {
Error: 'API endpoint not found',
};
res.status(400).end(JSON.stringify(err));
});
/**
* Starts server on port 3000, my particular setup requires a host of '0.0.0.0', but you can put anything you want here or leave the host argument out.
* @callback - Logs the start of the server session and port on which the server is listen.
*/
const listener = app.listen(port, '0.0.0.0', async () => {
monitor.ping({ state: 'ok', message: `API server listening on port ${port}.` });
try {
await db.connect();
} catch (err) {
console.error('[Error] Initial Upstash Redis connection check failed.', err);
}
youtube.startUpcomingVideoRecheckWorker();
console.log('API server listening on port ' + listener.address().port);
});
/**
* Instance of appReady, for graceful startup of server with PM2, etc.
* @requires util
*/
appReady(() => {
monitor.ping({ state: 'run', message: 'API server (re)started.' });
});
/**
* Instance of node-cleanup, for graceful shutdown of server.
* @requires node-cleanup
*/
cleanUp((code, signal) => {
db.quit();
monitor.ping({ status: 'complete', message: 'API server shut down.' })
});