/** * Driftline Analytics - Main HTTP entry point * * Anonymous analytics service for ATProto app views. */ import { Hono } from "https://esm.sh/hono@4.4.0"; import { cors } from "https://esm.sh/hono@4.4.0/cors"; import { runMigrations } from "./database/migrations.ts"; import { admin } from "./routes/admin.ts"; import { collector } from "./routes/collector.ts"; import { stats } from "./routes/stats.ts"; const app = new Hono(); // Re-throw errors to see full stack traces app.onError((err, _c) => { throw err; }); // Enable CORS for client-side tracking app.use( "*", cors({ origin: "*", allowMethods: ["GET", "POST", "OPTIONS"], allowHeaders: ["Content-Type", "X-API-Key"], }), ); // Run migrations on startup let migrationsRan = false; app.use("*", async (_c, next) => { if (!migrationsRan) { await runMigrations(); migrationsRan = true; } await next(); }); // Health check app.get("/", (c) => { return c.json({ service: "driftline-analytics", status: "ok", version: 1, }); }); // Mount routes app.route("/admin", admin); app.route("/collect", collector); app.route("/stats", stats); // Export for Valtown HTTP trigger export default app.fetch;