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 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import { OrdersCluster, OrdersIngress, OrdersNetwork, OrdersTable } from "./infra.ts"; |
| 7 | import SeedTask from "./SeedTask.ts"; |
| 8 | |
| 9 | /** |
| 10 | * The orders API: an `AWS.ECS.Service` in the TAGGED form — the class |
| 11 | * declares the service identity, and the default export is |
| 12 | * `Api.make(props, impl)`: a Layer pairing the props with an init Effect |
| 13 | * whose impl returns `{ fetch }`, bundled into a generated image |
| 14 | * (`main: import.meta.url`). The Service synthesizes its own task |
| 15 | * definition (task + execution roles, log group, ECR repository) from the |
| 16 | * same surface as `AWS.ECS.Task`. |
| 17 | * |
| 18 | * The stack provides the Layer and yields the class (see `alchemy.run.ts`): |
| 19 | * |
| 20 | * ```typescript |
| 21 | * const api = yield* Api; // with Effect.provide(ApiLive) |
| 22 | * ``` |
| 23 | * |
| 24 | * Ingress is the SHARED listener composed at the stack level (see |
| 25 | * `src/infra.ts`): this service only adds its own target group and a |
| 26 | * `path: "/api/*"` listener rule. The explicit `priority: 10` orders it |
| 27 | * ahead of `Web`'s catch-all `/*` rule. |
| 28 | * |
| 29 | * Bindings work exactly as on Lambda: |
| 30 | * - `AWS.DynamoDB.GetItem` / `Scan` grant read access to the orders table |
| 31 | * and inject its name into the container environment. |
| 32 | * - `AWS.ECS.RunTask(cluster, SeedTask)` grants `ecs:RunTask` on the seed |
| 33 | * task's definition (plus `iam:PassRole` on its roles), so |
| 34 | * `POST /api/seed` can launch the one-shot seeding task on Fargate. The |
| 35 | * cluster is explicit at the binding — a task definition is |
| 36 | * cluster-independent. |
| 37 | */ |
| 38 | export class Api extends AWS.ECS.Service<Api>()("Api") {} |
| 39 | |
| 40 | export default Api.make( |
| 41 | // Props are themselves an Effect so they can reference shared resources. |
| 42 | Effect.gen(function* () { |
| 43 | const cluster = yield* OrdersCluster; |
| 44 | const network = yield* OrdersNetwork; |
| 45 | const { listener } = yield* OrdersIngress; |
| 46 | return { |
| 47 | cluster, |
| 48 | main: import.meta.url, |
| 49 | image: "oven/bun:1", |
| 50 | port: 3000, |
| 51 | cpu: 256, |
| 52 | memory: 512, |
| 53 | desiredCount: 1, |
| 54 | // Build/run on ARM64 so an image built on an Apple Silicon host |
| 55 | // matches the Fargate runtime architecture (Graviton). |
| 56 | runtimePlatform: { cpuArchitecture: "ARM64", operatingSystemFamily: "LINUX" }, |
| 57 | vpcId: network.vpcId, |
| 58 | subnets: network.publicSubnetIds, |
| 59 | // Public subnets, no NAT: a public IP is required to pull the image. |
| 60 | assignPublicIp: true, |
| 61 | loadBalancer: { listener, rules: [{ path: "/api/*", priority: 10 }] }, |
| 62 | }; |
| 63 | }), |
| 64 | Effect.gen(function* () { |
| 65 | const table = yield* OrdersTable; |
| 66 | const network = yield* OrdersNetwork; |
| 67 | const cluster = yield* OrdersCluster; |
| 68 | const seedTask = yield* SeedTask; |
| 69 | |
| 70 | const scan = yield* AWS.DynamoDB.Scan(table); |
| 71 | const getItem = yield* AWS.DynamoDB.GetItem(table); |
| 72 | // The launch cluster is explicit at the binding — the task definition |
| 73 | // itself is cluster-independent. |
| 74 | const runSeedTask = yield* AWS.ECS.RunTask(cluster, seedTask); |
| 75 | |
| 76 | // First public subnet, bound into the environment so the runtime can |
| 77 | // build the seed task's awsvpc network configuration. |
| 78 | const SubnetId = yield* network.publicSubnetIds[0]!; |
| 79 | |
| 80 | return { |
| 81 | fetch: Effect.gen(function* () { |
| 82 | const request = yield* HttpServerRequest; |
| 83 | const url = new URL(request.originalUrl); |
| 84 | |
| 85 | // GET /api/orders — list everything in the table. |
| 86 | if (request.method === "GET" && url.pathname === "/api/orders") { |
| 87 | const result = yield* scan({}); |
| 88 | const orders = (result.Items ?? []).map((item) => ({ |
| 89 | id: item.pk?.S?.replace(/^order#/, ""), |
| 90 | customer: item.customer?.S, |
| 91 | total: item.total?.N ? Number(item.total.N) : undefined, |
| 92 | })); |
| 93 | return yield* HttpServerResponse.json({ count: orders.length, orders }); |
| 94 | } |
| 95 | |
| 96 | // GET /api/orders/<id> — read one order. |
| 97 | const match = url.pathname.match(/^\/api\/orders\/([^/]+)$/); |
| 98 | if (request.method === "GET" && match) { |
| 99 | const result = yield* getItem({ Key: { pk: { S: `order#${match[1]}` } } }); |
| 100 | if (!result.Item) { |
| 101 | return yield* HttpServerResponse.json({ error: "not found" }, { status: 404 }); |
| 102 | } |
| 103 | return yield* HttpServerResponse.json({ |
| 104 | id: match[1], |
| 105 | customer: result.Item.customer?.S, |
| 106 | total: result.Item.total?.N ? Number(result.Item.total.N) : undefined, |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | // POST /api/seed — launch the one-shot SeedTask on Fargate via the |
| 111 | // RunTask binding (cluster + task definition ARNs are injected). |
| 112 | if (request.method === "POST" && url.pathname === "/api/seed") { |
| 113 | const subnetId = yield* SubnetId; |
| 114 | const response = yield* runSeedTask({ |
| 115 | launchType: "FARGATE", |
| 116 | count: 1, |
| 117 | startedBy: "orders-api-seed", |
| 118 | networkConfiguration: { |
| 119 | awsvpcConfiguration: { |
| 120 | subnets: [subnetId], |
| 121 | // No securityGroups: the VPC default security group applies |
| 122 | // (all egress — enough to pull from ECR and reach DynamoDB). |
| 123 | assignPublicIp: "ENABLED", |
| 124 | }, |
| 125 | }, |
| 126 | }); |
| 127 | return yield* HttpServerResponse.json({ |
| 128 | taskArn: response.tasks?.[0]?.taskArn, |
| 129 | failures: response.failures ?? [], |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | // Everything else — including the ALB health check on "/" (health |
| 134 | // checks hit the container directly, not through listener rules). |
| 135 | return yield* HttpServerResponse.json({ ok: true, service: "orders-api" }); |
| 136 | }).pipe(Effect.orDie), |
| 137 | }; |
| 138 | }).pipe( |
| 139 | Effect.provide( |
| 140 | Layer.mergeAll(AWS.DynamoDB.ScanHttp, AWS.DynamoDB.GetItemHttp, AWS.ECS.RunTaskHttp), |
| 141 | ), |
| 142 | ), |
| 143 | ); |