-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisitor-badge.ts
57 lines (52 loc) · 1.69 KB
/
visitor-badge.ts
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
import { serve } from "https://deno.land/std@0.155.0/http/server.ts";
import { createHash } from "https://deno.land/std@0.155.0/hash/mod.ts";
import "https://unpkg.com/badgen";
const kv = await Deno.openKv();
serve(async (req: Request) => {
const u = new URL(req.url);
const page_id = getPageId(u);
if (!page_id) {
return Response.redirect("https://github.com/jwenjian/visitor-badge-deno", 302);
}
const page_checksum = checksumOfUrl(page_id);
const latest_count = await increase_count(page_checksum);
console.log(badgen);
const svg = badgen({
label: 'views',
status: "" + latest_count
});
const current = new Date();
let expire_time = new Date(current.getTime() - 600000);
return new Response(svg, {
status: 200,
headers: {
"Content-Type": "image/svg+xml;charset=utf-8",
"Cache-Control": "no-cache,max-age=0,no-store,s-maxage=0,proxy-revalidate",
"Expires": expire_time.toGMTString()
}
});
});
function checksumOfUrl(url: String) {
return createHash("md5").update(url).toString();
}
/**
* "/page.id.svg" -> "page.id"
*/
function getPageId(u: URL) {
if (u.pathname.length < 5) {
return null;
}
if (u.pathname.substring(0,1) !== "/") {
return null;
}
if (u.pathname.substring(u.pathname.length - 4) !== ".svg") {
return null;
}
return u.pathname.substring(1, u.pathname.length - 4);
}
async function increase_count(page_checksum: String) {
// 1n means 1 as a bigint
await kv.atomic().sum(["views", page_checksum], 1n).commit();
let r = await kv.get(["views", page_checksum]);
return r.value.value;
}