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, 06:00 UTC
| 1 | /** |
| 2 | * The hero's provider reel: one API, the same request handler, on eight |
| 3 | * providers. Adapted from the "Nine hosts, one API" roll in the talk deck |
| 4 | * (demos/effect-prezzy/intro/snippets/anywhere/Api*.ts; there the module is |
| 5 | * `Files`, here it's `Photos`, like the rest of the page). |
| 6 | * |
| 7 | * Props are plain objects: a prop accepts the resource's Effect directly |
| 8 | * (`cluster: Cluster`), so nothing has to be yielded first. |
| 9 | * |
| 10 | * ⟨0⟩ is the host, ⟨1⟩ the props it needs beyond `main`, and ⟨2⟩ the |
| 11 | * Photos Layer for that provider's storage. |
| 12 | */ |
| 13 | export const HOST_TEMPLATE = `export default ⟨0⟩( |
| 14 | "Api", |
| 15 | { main: import.meta.url⟨1⟩ }, |
| 16 | Effect.gen(function* () { |
| 17 | const photos = yield* Photos; |
| 18 | return { |
| 19 | fetch: Effect.gen(function* () { |
| 20 | const request = yield* HttpServerRequest; |
| 21 | yield* photos.upload(request.url, yield* request.text); |
| 22 | return HttpServerResponse.empty({ status: 201 }); |
| 23 | }), |
| 24 | }; |
| 25 | }).pipe(Effect.provide(⟨2⟩)), |
| 26 | );`; |
| 27 | |
| 28 | export interface HostResource { |
| 29 | id: string; |
| 30 | type: string; |
| 31 | bindings?: string[]; |
| 32 | } |
| 33 | |
| 34 | export interface Host { |
| 35 | /** Reel label: the provider. */ |
| 36 | label: string; |
| 37 | values: [string, string, string]; |
| 38 | resources: HostResource[]; |
| 39 | url?: string; |
| 40 | } |
| 41 | |
| 42 | export const HOSTS: Host[] = [ |
| 43 | { |
| 44 | label: "Cloudflare", |
| 45 | values: ["Cloudflare.Worker", "", "PhotosR2"], |
| 46 | resources: [ |
| 47 | { id: "Photos", type: "Cloudflare.R2.Bucket" }, |
| 48 | { id: "Api", type: "Cloudflare.Worker", bindings: ["Photos"] }, |
| 49 | ], |
| 50 | url: "https://api.my-app.workers.dev", |
| 51 | }, |
| 52 | { |
| 53 | label: "AWS", |
| 54 | values: ["AWS.Lambda.Function", "", "PhotosS3"], |
| 55 | resources: [ |
| 56 | { id: "Photos", type: "AWS.S3.Bucket" }, |
| 57 | { id: "Api", type: "AWS.Lambda.Function", bindings: ["Photos"] }, |
| 58 | ], |
| 59 | url: "https://7xk2q.lambda-url.us-east-1.on.aws", |
| 60 | }, |
| 61 | { |
| 62 | label: "GCP", |
| 63 | values: ["GCP.Run.Service", "", "PhotosGCS"], |
| 64 | resources: [ |
| 65 | { id: "Photos", type: "GCP.Storage.Bucket" }, |
| 66 | { id: "Api", type: "GCP.Run.Service", bindings: ["Photos"] }, |
| 67 | ], |
| 68 | url: "https://api-7xk2q-uc.a.run.app", |
| 69 | }, |
| 70 | { |
| 71 | label: "Kubernetes", |
| 72 | values: ["Kubernetes.Deployment", ", cluster: Cluster, port: 3000", "PhotosGCS"], |
| 73 | resources: [ |
| 74 | { id: "Photos", type: "GCP.Storage.Bucket" }, |
| 75 | { id: "Api", type: "Kubernetes.Deployment", bindings: ["Photos"] }, |
| 76 | ], |
| 77 | }, |
| 78 | { |
| 79 | label: "Fly", |
| 80 | values: ["Fly.Service", "", "PhotosTigris"], |
| 81 | resources: [ |
| 82 | { id: "Photos", type: "Fly.Bucket" }, |
| 83 | { id: "Api", type: "Fly.Service", bindings: ["Photos"] }, |
| 84 | ], |
| 85 | url: "https://my-app-api.fly.dev", |
| 86 | }, |
| 87 | { |
| 88 | label: "Railway", |
| 89 | values: ["Railway.Service", ", project: Project", "PhotosRailway"], |
| 90 | resources: [ |
| 91 | { id: "Project", type: "Railway.Project" }, |
| 92 | { id: "Photos", type: "Railway.Bucket" }, |
| 93 | { id: "Api", type: "Railway.Service", bindings: ["Photos"] }, |
| 94 | ], |
| 95 | url: "https://api-production.up.railway.app", |
| 96 | }, |
| 97 | { |
| 98 | label: "Hetzner", |
| 99 | values: ["Hetzner.Service", ", server: Box, port: 3000", "PhotosVolume"], |
| 100 | resources: [ |
| 101 | { id: "Box", type: "Hetzner.Server" }, |
| 102 | { id: "Photos", type: "Hetzner.Volume" }, |
| 103 | { id: "Api", type: "Hetzner.Service", bindings: ["Photos"] }, |
| 104 | ], |
| 105 | }, |
| 106 | { |
| 107 | label: "Neon", |
| 108 | values: ["Neon.Function", ", branch: Main", "PhotosNeon"], |
| 109 | resources: [ |
| 110 | { id: "Db", type: "Neon.Project" }, |
| 111 | { id: "Main", type: "Neon.Branch" }, |
| 112 | { id: "Photos", type: "Neon.Bucket" }, |
| 113 | { id: "Api", type: "Neon.Function", bindings: ["Photos"] }, |
| 114 | ], |
| 115 | }, |
| 116 | ]; |
| 117 | |
| 118 | /** The template with one host's values filled in. */ |
| 119 | export const hostSource = (values: readonly string[]) => |
| 120 | HOST_TEMPLATE.replace(/⟨(\d)⟩/g, (_, i: string) => values[+i]!); |
| 121 | |
| 122 | /** |
| 123 | * The same program for narrow screens: the props sit one per line and the |
| 124 | * request handler is folded to `handler`, so no line passes ~42 columns. |
| 125 | * Slot ⟨1⟩ holds the host's extra props on a line of their own. |
| 126 | */ |
| 127 | export const HOST_TEMPLATE_COMPACT = `export default ⟨0⟩( |
| 128 | "Api", |
| 129 | { |
| 130 | main: import.meta.url, |
| 131 | ⟨1⟩ |
| 132 | }, |
| 133 | Effect.gen(function* () { |
| 134 | const photos = yield* Photos; |
| 135 | return { fetch: handler }; |
| 136 | }).pipe(Effect.provide(⟨2⟩)), |
| 137 | );`; |
| 138 | |
| 139 | /** A host's values for the compact template. */ |
| 140 | export const compactValues = (values: readonly string[]) => [ |
| 141 | values[0]!, |
| 142 | values[1] ? `${values[1].replace(/^, /, "")},` : "", |
| 143 | values[2]!, |
| 144 | ]; |