Add request count logic and element update

This commit is contained in:
Aadi Desai 2024-03-31 14:42:28 +01:00
parent 2ef06644f2
commit 45c94dc0be
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM
2 changed files with 25 additions and 0 deletions

19
functions/requestcount.js Normal file
View file

@ -0,0 +1,19 @@
export const onRequest = async ({ env }) => {
const value = await fetch(env.TURSO_URL, {
method: "POST",
headers: {
Authorization: `Bearer ${env.TURSO_AUTH_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
requests: [
{ type: "execute", stmt: { sql: "select count from request_count where id = 1" } },
{ type: "close" },
],
}),
})
.then((res) => res.json())
.then((res) => res.results[0].response.result.rows[0][0].value)
.catch((err) => console.log(err));
return new Response(value, { status: 200 });
}

View file

@ -131,3 +131,9 @@ document.getElementById("go-now").addEventListener("click", () => {
alert("Please enter an Unstoppable Domain to go to"); alert("Please enter an Unstoppable Domain to go to");
} }
}); });
document.addEventListener("DOMContentLoaded", (e) => {
const counter = document.getElementById("request-count");
fetch("/requestcount").then(r => r.text())
.then(r => counter.textContent = Number(r.trim()).toLocaleString());
});