-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
68 lines (57 loc) · 1.69 KB
/
index.ts
File metadata and controls
68 lines (57 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { RPCHandler } from "@orpc/server/fetch";
import {
RequestHeadersPlugin,
ResponseHeadersPlugin,
} from "@orpc/server/plugins";
import { $ } from "bun";
import { Hono } from "hono";
import { inviteAdapter } from "./lib/adapters";
import { env } from "./lib/env";
import { logger } from "./lib/logger";
import { githubWebhookApp } from "./routes/github-webhook";
import staticApp from "./routes/static";
import { router } from "./rpc";
await $`bunx drizzle-kit migrate`;
const app = new Hono();
const rpcHandler = new RPCHandler(router, {
plugins: [new RequestHeadersPlugin(), new ResponseHeadersPlugin()],
});
// Health check endpoint
app.get("/health", (c) => {
return c.json({ status: "ok", timestamp: new Date().toISOString() });
});
// oRPC handler
app.on(["POST", "GET"], "/rpc/*", async (c) => {
const { matched, response } = await rpcHandler.handle(c.req.raw, {
prefix: "/rpc",
context: { requestUrl: c.req.url },
});
if (matched) return response;
return c.notFound();
});
// Keep github webhook as plain Hono route
app.route("/webhook", githubWebhookApp);
// Keep static serving
app.route("/", staticApp);
// Global error handler
app.onError((err, c) => {
logger.error({ err }, "Server error");
return c.json({ error: "Internal Server Error" }, 500);
});
// Fallback for unmatched routes
app.notFound((c) => {
return c.json({ error: "Not Found" }, 404);
});
// Start server
const server = Bun.serve({
port: env.PORT,
fetch: app.fetch,
});
logger.info({ url: server.url?.toString(), port: env.PORT }, "Server started");
if (env.REGISTRATION === "invite_only") {
inviteAdapter.createFirst().then((code) => {
if (code) {
logger.info({ code }, "First invite code created");
}
});
}