-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
43 lines (39 loc) · 1.27 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
const { badgen } = require('badgen')
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond with svg badge
* @param {Request} request
*/
async function handleRequest(request) {
var count = await ViewCounter.get('count')
if (count) {
var views = parseInt(JSON.parse(count)) + 1
await ViewCounter.put('count', JSON.stringify(views))
} else {
var views = 0
await ViewCounter.put('count', JSON.stringify(views))
}
const { searchParams } = new URL(request.url)
let label = searchParams.get('label') || 'Views'
let labelColor = searchParams.get('labelColor') || '555'
let color = searchParams.get('color') || 'blue'
let style = searchParams.get('style') || 'flat'
let scale = searchParams.get('scale') || 1
const svgString = badgen({
label: label,
labelColor: labelColor,
color: color,
style: style,
scale: scale,
status: views.toString(),
})
return new Response(svgString, {
headers: {
'content-type': 'image/svg+xml;charset=utf-8',
'access-control-allow-origin': '*',
'Cache-Control': ' no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'
},
})
}