-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (61 loc) · 1.89 KB
/
index.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
const fs = require('fs');
const Koa = require('koa');
const Dittorm = require('dittorm');
const { makeBadge } = require('badge-maker');
const app = new Koa();
async function counter(identifer) {
const { LEAN_ID, LEAN_KEY, LEAN_MASTER_KEY, LEAN_SERVER, DETA_PROJECT_KEY } = process.env;
let storage = 'leancloud';
if (DETA_PROJECT_KEY) {
storage = 'deta';
}
const counterModel = new Dittorm(storage)('badge', {
appId: LEAN_ID,
appKey: LEAN_KEY,
masterKey: LEAN_MASTER_KEY,
serverURL: LEAN_SERVER,
token: DETA_PROJECT_KEY,
});
const resp = await counterModel.select({ identifer });
if (!Array.isArray(resp) || !resp.length) {
const count = 1;
await counterModel.add({ identifer, count });
return count;
}
const count = resp[0].count + 1;
await counterModel.update({ count }, { identifer });
return count;
}
app.use(async (ctx, next) => {
ctx.svg = (badgeOption) => {
const svg = makeBadge(badgeOption);
ctx.type = 'image/svg+xml';
ctx.set('Cache-Control', 'no-cache,max-age=0');
ctx.set('Expires', new Date(Date.now() - 10 * 60 * 1000).toGMTString());
ctx.body = svg;
}
try {
await next();
} catch(e) {
ctx.svg({ label: 'Error', message: e.message, labelColor: '#555', color: '#007ec6' });
console.log(e);
}
});
app.use(async ctx => {
if (ctx.path !== '/badge') {
const homepage = await fs.readFileSync('./index.html', 'utf-8');
return ctx.body = homepage;
}
const { page_id: pageId, left_color: leftColor, right_color: rightColor, left_text: leftText } = ctx.query;
if (!pageId) {
throw new Error('Missing required param: page_id');
}
const latestCount = await counter(pageId);
return ctx.svg({
label: leftText || 'visitors',
message: latestCount.toString(),
labelColor: leftColor || '#595959',
color: rightColor || '#1283c3',
});
});
module.exports = app.callback();