| 1 | import * as Cloudflare from "@/Cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 4 | import { NeonHostProject } from "./db.ts"; |
| 5 | |
| 6 | class NeonHostContainer extends Cloudflare.Container<NeonHostContainer>()( |
| 7 | "NeonHostContainer", |
| 8 | Effect.gen(function* () { |
| 9 | const project = yield* NeonHostProject; |
| 10 | return { |
| 11 | context: `${import.meta.dirname}/../sqlreach/context`, |
| 12 | env: { |
| 13 | DATABASE_URL: project.pooledConnectionUri, |
| 14 | }, |
| 15 | observability: { logs: { enabled: true } }, |
| 16 | }; |
| 17 | }), |
| 18 | ) {} |
| 19 | |
| 20 | export class NeonHostContainerObject extends Cloudflare.DurableObject<NeonHostContainerObject>()( |
| 21 | "NeonHostContainerObject", |
| 22 | Effect.gen(function* () { |
| 23 | const container = yield* NeonHostContainer; |
| 24 | |
| 25 | return Effect.gen(function* () { |
| 26 | const { fetch } = yield* container.getTcpPort(8080); |
| 27 | |
| 28 | const get = (path: string) => |
| 29 | Effect.gen(function* () { |
| 30 | const response = yield* fetch( |
| 31 | HttpClientRequest.get(`http://container${path}`), |
| 32 | ); |
| 33 | return yield* response.text; |
| 34 | }); |
| 35 | |
| 36 | return { |
| 37 | getEnv: () => get("/env"), |
| 38 | getProbe: () => get("/probe"), |
| 39 | }; |
| 40 | }); |
| 41 | }).pipe( |
| 42 | Effect.provide( |
| 43 | Cloudflare.Containers.layer(NeonHostContainer, { |
| 44 | enableInternet: true, |
| 45 | }), |
| 46 | ), |
| 47 | ), |
| 48 | ) {} |