| 1 | import { Hono } from "hono"; |
| 2 | import { cors } from "hono/cors"; |
| 3 | import { loadEnv } from "./env"; |
| 4 | import type { Env } from "./env"; |
| 5 | import { openDatabase } from "./lib/db"; |
| 6 | import auth from "./routes/auth"; |
| 7 | import subscribe from "./routes/subscribe"; |
| 8 | |
| 9 | const env = loadEnv(); |
| 10 | |
| 11 | const db = openDatabase(env.DATABASE_PATH); |
| 12 | |
| 13 | type Variables = { env: Env; db: typeof db }; |
| 14 | |
| 15 | const app = new Hono<{ Variables: Variables }>(); |
| 16 | |
| 17 | // Inject env and db into all routes |
| 18 | app.use("*", async (c, next) => { |
| 19 | c.set("env", env); |
| 20 | c.set("db", db); |
| 21 | await next(); |
| 22 | }); |
| 23 | |
| 24 | // Health check |
| 25 | app.get("/api/health", (c) => c.json({ status: "ok" })); |
| 26 | |
| 27 | // OAuth routes |
| 28 | app.route("/oauth", auth); |
| 29 | |
| 30 | // Subscribe routes with CORS |
| 31 | app.use( |
| 32 | "/subscribe/*", |
| 33 | cors({ |
| 34 | origin: (origin) => origin, |
| 35 | credentials: true, |
| 36 | }), |
| 37 | ); |
| 38 | app.use( |
| 39 | "/subscribe", |
| 40 | cors({ |
| 41 | origin: (origin) => origin, |
| 42 | credentials: true, |
| 43 | }), |
| 44 | ); |
| 45 | app.route("/subscribe", subscribe); |
| 46 | |
| 47 | console.log(`Sequoia server listening on port ${env.PORT}`); |
| 48 | |
| 49 | export default { |
| 50 | port: env.PORT, |
| 51 | fetch: app.fetch, |
| 52 | }; |