| 1 | import * as Cloudflare from "@/Cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import { ApiKey } from "./secret.ts"; |
| 6 | import { secretRoutes } from "./secret-routes.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Effect-native invocation style: the Secret is bound inside the Worker init |
| 10 | * via `Cloudflare.SecretsStore.ReadSecret(ApiKey)` and the binding layer is |
| 11 | * provided with `Effect.provide(ReadSecretBinding)`. The `/secret` route drives |
| 12 | * the resulting {@link ReadSecretClient} (direct Effect, `.get()`, `.raw`). |
| 13 | */ |
| 14 | export default class EffectSecretWorker extends Cloudflare.Worker<EffectSecretWorker>()( |
| 15 | "EffectSecretBindingWorker", |
| 16 | { |
| 17 | main: import.meta.url, |
| 18 | workersDev: { enabled: true, previewsEnabled: false }, |
| 19 | }, |
| 20 | Effect.gen(function* () { |
| 21 | const secret = yield* ApiKey; |
| 22 | const apiKey = yield* Cloudflare.SecretsStore.ReadSecret(secret); |
| 23 | return { |
| 24 | fetch: Effect.gen(function* () { |
| 25 | const request = yield* HttpServerRequest; |
| 26 | const url = new URL(request.originalUrl, "http://x"); |
| 27 | const handled = yield* secretRoutes(apiKey, url); |
| 28 | return handled ?? HttpServerResponse.text("Not Found", { status: 404 }); |
| 29 | }), |
| 30 | }; |
| 31 | }).pipe(Effect.provide(Cloudflare.SecretsStore.ReadSecretBinding)), |
| 32 | ) {} |