The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | --- |
| 2 | title: "Part 5: Deploy to Cloudflare" |
| 3 | description: Host the bank on Durable Objects behind a Worker, run it locally, deploy it, and watch a balance change live. |
| 4 | sidebar: |
| 5 | order: 5 |
| 6 | --- |
| 7 | |
| 8 | Put the bank on Cloudflare. Every account, customer, view and policy becomes a Durable Object, and a Worker serves the Api. You need a [configured Cloudflare profile](/cloudflare/setup). |
| 9 | |
| 10 | ## Install the Cloudflare provider |
| 11 | |
| 12 | ```sh |
| 13 | bun add @alchemy.run/cloudflare |
| 14 | ``` |
| 15 | |
| 16 | ## Serve the Api from a Worker |
| 17 | |
| 18 | ```typescript |
| 19 | // src/BankWorker.ts |
| 20 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 21 | import * as DomainCloudflare from "@alchemy.run/cloudflare/Domain"; |
| 22 | import * as Effect from "effect/Effect"; |
| 23 | import * as Layer from "effect/Layer"; |
| 24 | import { Bank, BankHandlers } from "./Bank.model.ts"; |
| 25 | import { BankApi } from "./BankApi.api.ts"; |
| 26 | import { SessionLive } from "./Session.middleware.ts"; |
| 27 | |
| 28 | const BankLive = SessionLive.pipe( |
| 29 | Layer.provideMerge(Bank.layer(BankHandlers)), |
| 30 | Layer.provide(DomainCloudflare.layerDurableObjects()), |
| 31 | ); |
| 32 | |
| 33 | export default class BankWorker extends Cloudflare.Worker<BankWorker>()( |
| 34 | "BankWorker", |
| 35 | { main: import.meta.url }, |
| 36 | Effect.gen(function* () { |
| 37 | const api = yield* BankApi; |
| 38 | return { fetch: api.fetch }; |
| 39 | }).pipe(Effect.provide(BankLive)), |
| 40 | ) {} |
| 41 | ``` |
| 42 | |
| 43 | `DomainCloudflare.layerDurableObjects()` hosts the model: every aggregate, view and policy becomes a Durable Object class on this Worker, with no bindings to write. `SessionLive` is the middleware the Api's entries declare. `yield* BankApi` serves the Api, and its `fetch` becomes the Worker's. |
| 44 | |
| 45 | ## Declare the stack |
| 46 | |
| 47 | ```typescript |
| 48 | // alchemy.run.ts |
| 49 | import * as Alchemy from "alchemy"; |
| 50 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 51 | import * as Effect from "effect/Effect"; |
| 52 | import BankWorker from "./src/BankWorker.ts"; |
| 53 | |
| 54 | export default Alchemy.Stack( |
| 55 | "Bank", |
| 56 | { providers: Cloudflare.providers(), state: Cloudflare.state() }, |
| 57 | Effect.gen(function* () { |
| 58 | const bank = yield* BankWorker; |
| 59 | return { url: bank.url.as<string>() }; |
| 60 | }), |
| 61 | ); |
| 62 | ``` |
| 63 | |
| 64 | The stack deploys the Worker and returns its URL. |
| 65 | |
| 66 | ## Run it locally |
| 67 | |
| 68 | ```sh |
| 69 | bun alchemy dev |
| 70 | ``` |
| 71 | |
| 72 | `alchemy dev` runs the Worker and its Durable Objects on your machine and prints the URL, such as `http://localhost:1337`. |
| 73 | |
| 74 | ## Call it |
| 75 | |
| 76 | ```typescript |
| 77 | // scripts/try.ts |
| 78 | import * as Domain from "@alchemy.run/domain"; |
| 79 | import * as Effect from "effect/Effect"; |
| 80 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 81 | import * as Layer from "effect/Layer"; |
| 82 | import * as Stream from "effect/Stream"; |
| 83 | import { AccountId } from "../src/Account.id.ts"; |
| 84 | import { BankApi } from "../src/BankApi.api.ts"; |
| 85 | import { CustomerId } from "../src/Customer.id.ts"; |
| 86 | import { sessionClient } from "../src/Session.middleware.ts"; |
| 87 | |
| 88 | const [url, customerId = "c-1"] = process.argv.slice(2); |
| 89 | const accountId = AccountId.make(`${customerId}-main`); |
| 90 | |
| 91 | const program = Effect.gen(function* () { |
| 92 | const bank = yield* BankApi.client; |
| 93 | yield* bank.RegisterCustomer(CustomerId.make(customerId), { name: customerId }).pipe( |
| 94 | Effect.catchTag("AlreadyRegistered", () => Effect.void), |
| 95 | ); |
| 96 | const summaries = bank.AccountSummary.watch(accountId).pipe( |
| 97 | Stream.map(({ state }) => state), |
| 98 | Stream.filter((summary) => summary !== null && summary.owner !== null), |
| 99 | ); |
| 100 | // The policy opens the account shortly after registration: wait for it. |
| 101 | yield* Stream.runHead(summaries); |
| 102 | // Print the balance now and after every change, in the background. |
| 103 | yield* summaries.pipe( |
| 104 | Stream.runForEach((summary) => Effect.log(`balance: ${summary?.balance}`)), |
| 105 | Effect.forkChild, |
| 106 | ); |
| 107 | yield* Effect.sleep("1 second"); |
| 108 | yield* bank.DepositMoney(accountId, { amount: 100 }); |
| 109 | yield* Effect.sleep("1 second"); |
| 110 | yield* bank.WithdrawMoney(accountId, { amount: 30 }); |
| 111 | yield* Effect.sleep("2 seconds"); |
| 112 | }); |
| 113 | |
| 114 | const Connection = Layer.mergeAll(Domain.protocolHttp(url!), sessionClient(customerId)).pipe( |
| 115 | Layer.provide(FetchHttpClient.layer), |
| 116 | ); |
| 117 | |
| 118 | Effect.runPromise(program.pipe(Effect.scoped, Effect.provide(Connection))); |
| 119 | ``` |
| 120 | |
| 121 | The script registers a customer and waits for the policy to open their account, which happens shortly after registration. Then it watches the balance in the background while it deposits and withdraws. |
| 122 | |
| 123 | ```sh |
| 124 | bun scripts/try.ts http://localhost:1337 |
| 125 | ``` |
| 126 | |
| 127 | ``` |
| 128 | [12:00:01.402] INFO (#9): balance: 0 |
| 129 | [12:00:02.613] INFO (#9): balance: 100 |
| 130 | [12:00:03.804] INFO (#9): balance: 70 |
| 131 | ``` |
| 132 | |
| 133 | The watch prints the balance each time it changes. Over HTTP it checks for changes a few times a second, so the script pauses between commands to show each one; [Part 6](/domain/tutorial/part-6) switches to WebSockets, where the bank pushes every change. |
| 134 | |
| 135 | ## Deploy |
| 136 | |
| 137 | ```sh |
| 138 | bun alchemy deploy |
| 139 | ``` |
| 140 | |
| 141 | The deploy prints the Worker's `url`. Run the same script against it. |
| 142 | |
| 143 | ```sh |
| 144 | bun scripts/try.ts https://bank-bankworker-….workers.dev |
| 145 | ``` |
| 146 | |
| 147 | The bank now runs on Cloudflare, with one Durable Object per account. |
| 148 | |
| 149 | :::note |
| 150 | Next, [build the browser app](/domain/tutorial/part-6). How the model maps onto Durable Objects is in [Cloudflare Durable Objects](/domain/cloudflare). |
| 151 | ::: |