| 1 | import * as Cloudflare from "@/Cloudflare"; |
| 2 | import * as Neon from "@/Neon"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Redacted from "effect/Redacted"; |
| 6 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 7 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 8 | import { |
| 9 | backendAuth, |
| 10 | backendBranch, |
| 11 | backendDataApi, |
| 12 | backendGateway, |
| 13 | } from "./backend-resources.ts"; |
| 14 | |
| 15 | const bindings = Layer.mergeAll( |
| 16 | Neon.ConnectAuthHttp, |
| 17 | Neon.QueryDataApiHttp, |
| 18 | Neon.QueryAIGatewayHttp, |
| 19 | ).pipe(Layer.provide(FetchHttpClient.layer)); |
| 20 | |
| 21 | export default class BackendHttpWorker extends Cloudflare.Worker<BackendHttpWorker>()( |
| 22 | "BackendHttpWorker", |
| 23 | { |
| 24 | main: import.meta.url, |
| 25 | }, |
| 26 | Effect.gen(function* () { |
| 27 | const auth = yield* Neon.ConnectAuth(backendAuth); |
| 28 | const data = yield* Neon.QueryDataApi(backendDataApi); |
| 29 | const ai = yield* Neon.QueryAIGateway(backendGateway); |
| 30 | const shared = yield* Neon.QueryAIGateway( |
| 31 | Effect.gen(function* () { |
| 32 | return yield* Neon.AIGateway("SharedGateway", { |
| 33 | branch: yield* backendBranch, |
| 34 | }); |
| 35 | }), |
| 36 | ); |
| 37 | return { |
| 38 | fetch: Effect.gen(function* () { |
| 39 | return yield* HttpServerResponse.json({ |
| 40 | authUrl: yield* auth.baseUrl, |
| 41 | jwksUrl: yield* auth.jwksUrl, |
| 42 | dataUrl: yield* data.baseUrl, |
| 43 | aiUrl: yield* ai.baseUrl, |
| 44 | hasToken: Redacted.value(yield* ai.token).length > 0, |
| 45 | sharedToken: |
| 46 | Redacted.value(yield* ai.token) === |
| 47 | Redacted.value(yield* shared.token), |
| 48 | }); |
| 49 | }), |
| 50 | }; |
| 51 | }).pipe(Effect.provide(bindings)), |
| 52 | ) {} |