packages/server/src/index.ts 1.3 K raw
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
import recommend from "./routes/recommend";
9
10
const env = loadEnv();
11
12
const db = openDatabase(env.DATABASE_PATH);
13
14
type Variables = { env: Env; db: typeof db };
15
16
const app = new Hono<{ Variables: Variables }>();
17
18
// Inject env and db into all routes
19
app.use("*", async (c, next) => {
20
	c.set("env", env);
21
	c.set("db", db);
22
	await next();
23
});
24
25
// Health check
26
app.get("/api/health", (c) => c.json({ status: "ok" }));
27
28
// OAuth routes
29
app.route("/oauth", auth);
30
31
// Subscribe routes with CORS
32
app.use(
33
	"/subscribe/*",
34
	cors({
35
		origin: (origin) => origin,
36
		credentials: true,
37
	}),
38
);
39
app.use(
40
	"/subscribe",
41
	cors({
42
		origin: (origin) => origin,
43
		credentials: true,
44
	}),
45
);
46
app.route("/subscribe", subscribe);
47
48
// Recommend routes with CORS
49
app.use(
50
	"/recommend/*",
51
	cors({
52
		origin: (origin) => origin,
53
		credentials: true,
54
	}),
55
);
56
app.use(
57
	"/recommend",
58
	cors({
59
		origin: (origin) => origin,
60
		credentials: true,
61
	}),
62
);
63
app.route("/recommend", recommend);
64
65
console.log(`Sequoia server listening on port ${env.PORT}`);
66
67
export default {
68
	port: env.PORT,
69
	fetch: app.fetch,
70
};