| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 4 | import { DataBucket, Tweets } from "./bound-resources.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Second deploy step of {@link ./service.ts}: same service, Storage |
| 8 | * bindings removed (the bucket stays declared). The runtime service |
| 9 | * account must lose its grants on the bucket. |
| 10 | */ |
| 11 | export default class BoundService extends GCP.Function<BoundService>()( |
| 12 | "BoundService", |
| 13 | { |
| 14 | main: import.meta.url, |
| 15 | location: "us-central1", |
| 16 | invokerIamDisabled: true, |
| 17 | }, |
| 18 | Effect.gen(function* () { |
| 19 | const tweets = yield* Tweets; |
| 20 | yield* DataBucket; |
| 21 | const publish = yield* GCP.PubSub.Publish(tweets); |
| 22 | |
| 23 | return { |
| 24 | fetch: Effect.gen(function* () { |
| 25 | const { messageIds } = yield* publish({ |
| 26 | body: { messages: [{ data: btoa("again") }] }, |
| 27 | }).pipe(Effect.orDie); |
| 28 | return yield* HttpServerResponse.json({ |
| 29 | published: (messageIds?.length ?? 0) > 0, |
| 30 | step: 2, |
| 31 | }); |
| 32 | }), |
| 33 | }; |
| 34 | }).pipe(Effect.provide(GCP.PubSub.PublishHttp)), |
| 35 | ) {} |