-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.ts
More file actions
50 lines (41 loc) · 1.27 KB
/
worker.ts
File metadata and controls
50 lines (41 loc) · 1.27 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
/**
* Cloudflare Workers entry point for the API.
*
* This module configures the core API app with Cloudflare Workers-specific
* context initialization, including Hyperdrive database bindings and
* authentication setup.
*
* SPDX-FileCopyrightText: 2014-present Kriasoft
* SPDX-License-Identifier: MIT
*/
import { Hono } from "hono";
import app from "./lib/app.js";
import { createAuth } from "./lib/auth.js";
import type { AppContext } from "./lib/context.js";
import { createDb } from "./lib/db.js";
import type { Env } from "./lib/env.js";
type CloudflareEnv = {
HYPERDRIVE_CACHED: Hyperdrive;
HYPERDRIVE_DIRECT: Hyperdrive;
} & Env;
// Create a Hono app with Cloudflare Workers context
const worker = new Hono<{
Bindings: CloudflareEnv;
Variables: AppContext["Variables"];
}>();
// Initialize shared context for all requests
worker.use("*", async (c, next) => {
// Initialize database using Neon via Hyperdrive
const db = createDb(c.env.HYPERDRIVE_CACHED);
const dbDirect = createDb(c.env.HYPERDRIVE_DIRECT);
// Initialize auth
const auth = createAuth(db, c.env);
// Set context variables
c.set("db", db);
c.set("dbDirect", dbDirect);
c.set("auth", auth);
await next();
});
// Mount the core API app
worker.route("/", app);
export default worker;