/
main.js
61 lines (60 loc) · 2.05 KB
/
main.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
const crypto = require('crypto');
const h = require('render-html-rpf');
const fetch = require('node-fetch');
const mapProperties = require('map-properties');
module.exports = async () => {
api.get('/re/analytics', async ctx => {
const [countsRes] = await multiExecAsync(client, multi => {
multi.hgetall([config.redisNamespace, 'count:h'].join(':'));
});
const counts = mapProperties(countsRes || {}, value => parseInt(value));
const analytics = {counts};
if (/(Mobile)/.test(ctx.get('user-agent'))) {
ctx.body = h.page({
title: 'rejson',
heading: 'Analytics',
content: [{
name: 'pre',
content: JSON.stringify(analytics, null, 2)}
],
footerLink: 'https://github.com/evanx/rejson'
});
} else {
ctx.body = analytics;
}
});
api.get('/re/*', async ctx => {
const path = ctx.params[0];
const parts = path.split('/');
const key = [...parts, 'j'].join(':')
const sha = crypto.createHash('sha1').update(key).digest('hex');
const [content] = await multiExecAsync(client, multi => {
multi.get(key);
multi.hincrby([config.redisNamespace, 'count:h'].join(':'), 'req', 1);
});
if (content) {
ctx.set('Content-Type', 'application/json');
ctx.body = content;
return;
}
if (false) {
throw new Error('test');
}
const refileUrl = [
'https://' + config.refileDomain,
'key',
sha.substring(0, 3),
parts.join('-') + '.json'
].join('/');
logger.debug({key, sha, refileUrl});
if (false) {
ctx.redirect(refileUrl);
}
const fetchRes = await fetch(refileUrl);
if (fetchRes.status !== 200) {
ctx.statusCode = fetchRes.status;
} else {
ctx.body = await fetchRes.json();
}
});
}