forked from daniloc/airtable-api-proxy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
269 lines (227 loc) · 10.1 KB
/
server.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
/**
* @file Express server with IP validation middleware and graceful cleanup
* @author Avana Vana <[email protected]>
* @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 cron = require('./cron');
const esovdb = require('./esovdb');
const webhooks = require('./webhooks');
const youtube = require('./youtube');
const zotero = require('./zotero');
const app = express();
const middleware = {
/**
* Middleware for blackquerying or whitequerying 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['esovdb-key'] && req.headers['esovdb-key'] === process.env.ESOVDB_KEY) {
console.log('ESOVDB key validated.');
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 {
console.error(`Unauthorized attempted access of ${req.path} without a valid ESOVDB key.`);
res.status(401).send('Unauthorized access. Visit https://rapidapi.com/avanavana/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 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 selecting a single video ESOVDB, by its ESOVDB Airtable ID, returns JSON. Used with the premium header 'esovdb-no-cache', 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 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.status >= 400) throw new Error('[ERROR] Unable to respond to Discord user submission.')
res.status(200).send(response.config.data)
});
/**
* 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.status >= 400) throw new Error('[ERROR] Unable to respond to Twitter webhook event.')
res.status(200).send(response.config.data)
})
.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);
});
/**
* 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(3000, '0.0.0.0', () => {
monitor.ping({ state: 'ok', message: 'API server listen on port 3000.' });
db.connect();
cron.startJobs([ cron.getLatest ]);
console.log('API proxy listen 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();
cron.destroyJobs();
monitor.ping({ status: 'complete', message: 'API server shut down.' })
});