mirror of
https://github.com/4ev-link/4ev.link.git
synced 2026-01-14 00:28:05 +00:00
Feat: Log analytics only if enabled via KV prefix
This commit is contained in:
@@ -1,18 +1,17 @@
|
|||||||
export async function onRequestGet({ request, params, env, next, waitUntil }) {
|
export async function onRequestGet({ request, params, env, next, waitUntil }) {
|
||||||
if (['abuse', 'admin', 'api', 'dash', 'acceptable-use'].includes(params.slug)) return next();
|
if (['abuse', 'admin', 'api', 'dash', 'acceptable-use'].includes(params.slug)) return next();
|
||||||
try {
|
try {
|
||||||
const dest = await env.KV_EV.get(params.slug);
|
let dest = await env.KV_EV.get(params.slug);
|
||||||
if (dest) {
|
if (dest?.startsWith('✺')) {
|
||||||
waitUntil((async () => {
|
waitUntil((async () => {
|
||||||
let ref = 'direct';
|
let ref = 'direct';
|
||||||
try { ref = new URL(request.headers.get('Referer')).hostname } catch {}
|
try { ref = new URL(request.headers.get('Referer')).hostname } catch {}
|
||||||
const stmt = `
|
const stmt = `INSERT INTO analytics (slug, referrer, count) VALUES (?, ?, 1) ON CONFLICT(slug, referrer) DO UPDATE SET count = count + 1;`;
|
||||||
INSERT INTO analytics (slug, referrer, count) VALUES (?, ?, 1)
|
|
||||||
ON CONFLICT(slug, referrer) DO UPDATE SET count = count + 1;
|
|
||||||
`;
|
|
||||||
await env.D1_EV.prepare(stmt).bind(params.slug, ref).run().catch(console.error);
|
await env.D1_EV.prepare(stmt).bind(params.slug, ref).run().catch(console.error);
|
||||||
})());
|
})());
|
||||||
|
dest = dest.substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = dest ? `https://${dest}` : new URL('/', request.url).href;
|
const url = dest ? `https://${dest}` : new URL('/', request.url).href;
|
||||||
return Response.redirect(url, dest ? 301 : 302);
|
return Response.redirect(url, dest ? 301 : 302);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user