-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (66 loc) · 2.18 KB
/
app.js
File metadata and controls
84 lines (66 loc) · 2.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
const { fork } = require('child_process');
const fs = require('fs');
const express = require('express');
const app = express()
const PORT = process.env.PORT || 9000
const uuidv4 = require('uuid/v4');
const redis = require("./redis-client");
const utils = require('./utils/index');
const showdown = require('showdown');
const converter = new showdown.Converter();
/* Configure express app */
app.use(express.json());
/* Serve API instructions */
app.get('/', function(req, res) {
fs.readFile(__dirname + '/welcome.md', 'utf-8', function(err, data) {
return res.status(500).send({ error: 'Server error' })
res.send(converter.makeHtml(data));
});
});
/* Get status of an existing job */
app.get('/status/:id', async function(req, res) {
const status = {};
const record = JSON.parse(await redis.getCrawlRecord(req.params.id));
if (utils.isnull(record)) {
return res.status(404).send({ error: 'Crawl ID does not exist' })
}
status['status'] = record.status;
status['unique urls crawled'] = (Object.keys(record.urls)).length;
return res.json(status);
});
/* Get url results of an existing job */
app.get('/result/:id', async function(req, res) {
const record = JSON.parse(await redis.getCrawlRecord(req.params.id));
if (utils.isnull(record)) {
return res.status(404).send({ error: 'Crawl ID does not exist' })
}
return res.json(record.urls);
});
/* Create a new crawl job */
app.post('/', async function (req, res) {
// TODO: Validate seed url in request body
// Create unique id for redis key
const id = uuidv4();
// fork another process
const process = fork('./crawler.js');
const msg = {
id: id,
seedurl: req.body.seedurl,
levels: req.body.levels,
status: 'pending',
urls: {}
}
// Put new record into datastore
try {
await redis.setCrawlRecord(id, JSON.stringify(msg));
} catch(err) {
console.log('caught error when trying to set the record for first time');
throw err;
}
// Send message to crawler to start crawling
process.send(msg);
return res.json(msg);
});
app.listen(PORT, () => console.log(`Crawler ready on port ${PORT}...`))
// Export for testing
module.exports = app;