-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (44 loc) · 1.41 KB
/
index.js
File metadata and controls
50 lines (44 loc) · 1.41 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
const fs = require('fs');
const http = require('http');
const port = +process.argv[2] || 3000;
const client = require('redis').createClient();
const cardsData = fs.readFileSync('./cards.json');
const parsedCards = JSON.parse(cardsData);
const cards = Array(...parsedCards.map(c => Buffer.from(JSON.stringify(c)), "ascii"));
const CARD_ADD_URL = '/card_add?';
const READY_URL = '/ready';
const OK = 200;
const ERROR = 400;
const DONE = Buffer.from('{"id": "ALL CARDS"}', 'ascii');
const READY = Buffer.from('{"ready":true}', 'ascii');
const done = {};
const listener = async function(req, res) {
if (req.url.startsWith(CARD_ADD_URL)) {
res.writeHead(OK);
const key = req.url.substring(CARD_ADD_URL.length);
if (!done[key]) {
const index = await client.incr(key) - 1;
if (index < cards.length) {
res.end(cards[index]);
return;
}
done[key] = 1;
}
res.end(DONE);
return;
} else if (req.url === READY_URL) {
res.writeHead(OK);
res.end(READY);
return;
}
res.writeHead(ERROR);
res.end();
}
const server = http.createServer(listener);
client.on('ready', () => {
server.listen(port, '0.0.0.0', () => {
console.log(`Example app listening at http://0.0.0.0:${port}`);
});
});
client.on('error', (err) => console.log('Redis Client Error', err));
client.connect();