| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import InvokeCallee from "./invoke-callee.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Public Effect-native Cloud Run Service that calls the private |
| 9 | * {@link InvokeCallee} through `GCP.Run.InvokeService`. |
| 10 | */ |
| 11 | export default class InvokeCaller extends GCP.Function<InvokeCaller>()( |
| 12 | "InvokeCaller", |
| 13 | { |
| 14 | main: import.meta.url, |
| 15 | location: "us-central1", |
| 16 | invokerIamDisabled: true, |
| 17 | }, |
| 18 | Effect.gen(function* () { |
| 19 | const callee = yield* GCP.Run.InvokeService(InvokeCallee); |
| 20 | return { |
| 21 | fetch: Effect.gen(function* () { |
| 22 | const request = yield* HttpServerRequest; |
| 23 | const path = new URL(request.originalUrl, "http://localhost").pathname; |
| 24 | const response = |
| 25 | path === "/post" |
| 26 | ? yield* callee.fetch("/echo", { |
| 27 | method: "POST", |
| 28 | headers: { "content-type": "text/plain" }, |
| 29 | body: "ping", |
| 30 | }) |
| 31 | : yield* callee.fetch("/hello"); |
| 32 | return yield* HttpServerResponse.json({ |
| 33 | status: response.status, |
| 34 | body: response.status === 200 ? yield* response.json : undefined, |
| 35 | }); |
| 36 | }).pipe(Effect.orDie), |
| 37 | }; |
| 38 | }).pipe(Effect.provide(GCP.Run.InvokeServiceHttp)), |
| 39 | ) {} |