forked from OpenTermsArchive/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-common.mjs
39 lines (30 loc) · 1.14 KB
/
server-common.mjs
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
import config from 'config';
import Archivist from './src/archivist/index.js';
export async function handler (req, res) {
// eslint-disable-next-line no-unused-vars
let body = '';
const header = req.headers.authorization || ''; // get the auth header
const token = header.split(/\s+/).pop() || ''; // and the encoded auth token
console.log(`checking token "${token}" (should match "${process.env.API_SECRET}")`);
if (token !== process.env.API_SECRET) {
console.log('api token check fail');
res.writeHead(401);
res.end('Please send an Authorization header with the API_SECRET from this script\'s run environment\n');
return;
}
console.log('api token check success');
req.on('data', chunk => {
body += chunk;
});
req.on('end', async () => {
const options = JSON.parse(body);
console.log(options);
const archivist = new Archivist({
recorderConfig: config.get('@opentermsarchive/engine.recorder'),
fetcherConfig: config.get('@opentermsarchive/engine.fetcher'),
});
const results = await archivist.crawl(options);
res.write(JSON.stringify(results));
res.end();
});
}