Skip to content

Commit 8e0c845

Browse files
Merge pull request #1 from Keshav-writes-code/feat/analytics
Feat/analytics
2 parents 7d46071 + 1d11b21 commit 8e0c845

7 files changed

Lines changed: 7580 additions & 11 deletions

File tree

astro.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22
import { defineConfig } from "astro/config";
33
import starlight from "@astrojs/starlight";
4+
import { analytics } from "./src/integrations/analytics";
45

56
// https://astro.build/config
67
export default defineConfig({
@@ -36,6 +37,7 @@ export default defineConfig({
3637
"./src/styles/custom.css",
3738
],
3839
}),
40+
analytics(),
3941
],
4042
site: "https://keshav.is-a.dev",
4143
base: "FreqKnow",

server/bun.lock

Lines changed: 58 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"cf-typegen": "wrangler types --env-interface CloudflareBindings"
88
},
99
"dependencies": {
10+
"@libsql/client": "^0.15.10",
1011
"hono": "^4.9.1"
1112
},
1213
"devDependencies": {

server/src/index.ts

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
1-
import { Hono } from 'hono'
1+
import { createClient, Client } from "@libsql/client";
2+
import { Hono } from "hono";
3+
import { cors } from "hono/cors";
24

3-
const app = new Hono()
5+
const app = new Hono();
46

5-
app.get('/', (c) => {
6-
return c.text('Hello Hono!')
7-
})
7+
// Define global typing for Turso client
8+
declare global {
9+
var tursoClient: Client | undefined;
10+
}
811

9-
export default app
12+
app.use(
13+
cors({
14+
origin: ["https://keshav.is-a.dev"],
15+
allowMethods: ["POST"],
16+
}),
17+
);
18+
19+
app.get("*", async ({ env }, next) => {
20+
if (!globalThis.tursoClient) {
21+
if (!env.TURSO_AUTH_TOKEN || !env.TURSO_DATABASE_URL) {
22+
throw new Error("Turso API Keys Required");
23+
}
24+
globalThis.tursoClient = createClient({
25+
url: env.TURSO_DATABASE_URL,
26+
authToken: env.TURSO_AUTH_TOKEN,
27+
});
28+
}
29+
await next();
30+
});
31+
32+
app.get("/track", async (c) => {
33+
try {
34+
const turso = globalThis.tursoClient;
35+
if (!turso) throw new Error("Turso Client is not Initialized");
36+
await turso.execute(`
37+
UPDATE Analytics
38+
SET views = views + 1
39+
WHERE rowid = 1;
40+
`);
41+
} catch (error) {
42+
console.error("Database error:", error);
43+
return c.text("Database query failed", 500);
44+
}
45+
return c.text("Succesfully Incremented");
46+
});
47+
48+
export default app;

0 commit comments

Comments
 (0)