| 1 | /** |
| 2 | * The Worker that serves the API: Better Auth's routes under `/api/auth`, |
| 3 | * the git server for everything else. `src/worker.ts` fronts it on the |
| 4 | * website's origin. |
| 5 | */ |
| 6 | import * as HttpRouter from "effect/http/HttpRouter"; |
| 7 | |
| 8 | import { CloudflareD1 } from "@alchemy.run/better-auth/CloudflareD1"; |
| 9 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 10 | import * as Git from "alchemy/Git"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 13 | import { Auth, AuthDb } from "./auth.ts"; |
| 14 | import { HttpLive } from "./git.ts"; |
| 15 | |
| 16 | export default class GitHost extends Cloudflare.Worker<GitHost>()( |
| 17 | "GitHost", |
| 18 | { |
| 19 | main: import.meta.url, |
| 20 | ...Git.GIT_WORKER_OPTIONS, |
| 21 | observability: { enabled: true }, |
| 22 | }, |
| 23 | Effect.gen(function* () { |
| 24 | const auth = yield* Auth; |
| 25 | const fetch = yield* HttpRouter.toHttpEffect(HttpLive); |
| 26 | return { |
| 27 | fetch: Effect.scoped( |
| 28 | Effect.gen(function* () { |
| 29 | const request = yield* HttpServerRequest; |
| 30 | if (request.url.startsWith("/api/auth")) return yield* auth.fetch; |
| 31 | return yield* fetch; |
| 32 | }), |
| 33 | ), |
| 34 | }; |
| 35 | }).pipe(Effect.provide(CloudflareD1(AuthDb))), |
| 36 | ) {} |