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 Duration from "effect/Duration"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import path from "pathe"; |
| 8 | import { getDefaultVpc } from "../DefaultVpc.ts"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 11 | |
| 12 | export class CloudMapTestFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()( |
| 13 | "CloudMapTestFunction", |
| 14 | ) {} |
| 15 | |
| 16 | /** |
| 17 | * Resolve the default VPC for the fixture's private DNS namespace. |
| 18 | * |
| 19 | * Runtime-guarded: the Lambda runtime re-executes this props effect on cold |
| 20 | * start, but the namespace's VPC association is deploy-time-only |
| 21 | * configuration and the execution role has no ec2:Describe* permissions — |
| 22 | * return a placeholder there without touching the EC2 API. |
| 23 | */ |
| 24 | const resolveFixtureVpc = Effect.gen(function* () { |
| 25 | if (globalThis.__ALCHEMY_RUNTIME__) { |
| 26 | return "vpc-unused-at-runtime"; |
| 27 | } |
| 28 | // Deploy-time only: a missing default VPC is an unrecoverable fixture |
| 29 | // setup failure, so orDie rather than leak EC2's error channel into the |
| 30 | // Function program (whose error channel must stay ConfigError). |
| 31 | return (yield* getDefaultVpc.pipe(Effect.orDie)).vpcId as string; |
| 32 | }); |
| 33 | |
| 34 | export default CloudMapTestFunction.make( |
| 35 | { |
| 36 | main, |
| 37 | functionUrl: true, |
| 38 | // several routes fan out Cloud Map API calls; the 3s default is too tight |
| 39 | timeout: Duration.seconds(30), |
| 40 | }, |
| 41 | Effect.gen(function* () { |
| 42 | const vpc = yield* resolveFixtureVpc; |
| 43 | const namespace = yield* AWS.CloudMap.PrivateDnsNamespace("FixtureNamespace", { |
| 44 | name: "alchemy-cloudmap-fixture.local", |
| 45 | vpc, |
| 46 | }); |
| 47 | const service = yield* AWS.CloudMap.Service("FixtureService", { |
| 48 | namespaceId: namespace.namespaceId, |
| 49 | name: "backend", |
| 50 | dnsRecords: [{ type: "A", ttl: "10 seconds" }], |
| 51 | routingPolicy: "MULTIVALUE", |
| 52 | attributes: { tier: "backend", version: "1" }, |
| 53 | }); |
| 54 | // push-based health: instances report their own status via |
| 55 | // UpdateInstanceCustomHealthStatus |
| 56 | const customService = yield* AWS.CloudMap.Service("FixtureCustomService", { |
| 57 | namespaceId: namespace.namespaceId, |
| 58 | name: "custom-health", |
| 59 | dnsRecords: [{ type: "A", ttl: "10 seconds" }], |
| 60 | routingPolicy: "MULTIVALUE", |
| 61 | healthCheckCustomConfig: {}, |
| 62 | }); |
| 63 | |
| 64 | const discoverInstances = yield* AWS.CloudMap.DiscoverInstances(service); |
| 65 | const discoverInstancesRevision = yield* AWS.CloudMap.DiscoverInstancesRevision(service); |
| 66 | const registerInstance = yield* AWS.CloudMap.RegisterInstance(service); |
| 67 | const deregisterInstance = yield* AWS.CloudMap.DeregisterInstance(service); |
| 68 | const getInstance = yield* AWS.CloudMap.GetInstance(service); |
| 69 | const listInstances = yield* AWS.CloudMap.ListInstances(service); |
| 70 | const getServiceAttributes = yield* AWS.CloudMap.GetServiceAttributes(service); |
| 71 | const getOperation = yield* AWS.CloudMap.GetOperation(); |
| 72 | const registerCustomInstance = yield* AWS.CloudMap.RegisterInstance(customService); |
| 73 | const deregisterCustomInstance = yield* AWS.CloudMap.DeregisterInstance(customService); |
| 74 | const updateCustomHealth = yield* AWS.CloudMap.UpdateInstanceCustomHealthStatus(customService); |
| 75 | const getCustomHealthStatus = yield* AWS.CloudMap.GetInstancesHealthStatus(customService); |
| 76 | const ServiceId = yield* service.serviceId; |
| 77 | const CustomServiceId = yield* customService.serviceId; |
| 78 | const NamespaceName = yield* service.namespaceName; |
| 79 | const ServiceName = yield* service.serviceName; |
| 80 | |
| 81 | return { |
| 82 | fetch: Effect.gen(function* () { |
| 83 | const request = yield* HttpServerRequest; |
| 84 | const url = new URL(request.originalUrl); |
| 85 | const pathname = url.pathname; |
| 86 | |
| 87 | if (request.method === "GET" && pathname === "/info") { |
| 88 | const serviceId = yield* ServiceId; |
| 89 | const customServiceId = yield* CustomServiceId; |
| 90 | const namespaceName = yield* NamespaceName; |
| 91 | const serviceName = yield* ServiceName; |
| 92 | return yield* HttpServerResponse.json({ |
| 93 | serviceId, |
| 94 | customServiceId, |
| 95 | namespaceName, |
| 96 | serviceName, |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | if (request.method === "GET" && pathname === "/discover") { |
| 101 | const health = url.searchParams.get("health") ?? "ALL"; |
| 102 | const result = yield* discoverInstances({ |
| 103 | HealthStatus: health as "HEALTHY" | "UNHEALTHY" | "ALL", |
| 104 | }); |
| 105 | return yield* HttpServerResponse.json({ |
| 106 | instances: (result.Instances ?? []).map((instance) => ({ |
| 107 | instanceId: instance.InstanceId, |
| 108 | healthStatus: instance.HealthStatus, |
| 109 | attributes: instance.Attributes ?? {}, |
| 110 | })), |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | if (request.method === "GET" && pathname === "/revision") { |
| 115 | const result = yield* discoverInstancesRevision(); |
| 116 | return yield* HttpServerResponse.json({ |
| 117 | revision: result.InstancesRevision, |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | if (request.method === "GET" && pathname === "/instance") { |
| 122 | const instanceId = url.searchParams.get("id") ?? ""; |
| 123 | const result = yield* getInstance({ InstanceId: instanceId }); |
| 124 | return yield* HttpServerResponse.json({ |
| 125 | instanceId: result.Instance?.Id, |
| 126 | attributes: result.Instance?.Attributes ?? {}, |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | if (request.method === "GET" && pathname === "/instances") { |
| 131 | const result = yield* listInstances(); |
| 132 | return yield* HttpServerResponse.json({ |
| 133 | instances: (result.Instances ?? []).map((instance) => ({ |
| 134 | instanceId: instance.Id, |
| 135 | attributes: instance.Attributes ?? {}, |
| 136 | })), |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | if (request.method === "GET" && pathname === "/service-attributes") { |
| 141 | const result = yield* getServiceAttributes(); |
| 142 | return yield* HttpServerResponse.json({ |
| 143 | attributes: result.ServiceAttributes?.Attributes ?? {}, |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | if (request.method === "GET" && pathname === "/operation") { |
| 148 | const operationId = url.searchParams.get("id") ?? ""; |
| 149 | const result = yield* getOperation({ OperationId: operationId }); |
| 150 | return yield* HttpServerResponse.json({ |
| 151 | status: result.Operation?.Status, |
| 152 | errorMessage: result.Operation?.ErrorMessage, |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | if (request.method === "POST" && pathname === "/register") { |
| 157 | const body = (yield* request.json) as unknown as { |
| 158 | instanceId: string; |
| 159 | attributes: Record<string, string>; |
| 160 | }; |
| 161 | const result = yield* registerInstance({ |
| 162 | InstanceId: body.instanceId, |
| 163 | Attributes: body.attributes, |
| 164 | }); |
| 165 | return yield* HttpServerResponse.json({ |
| 166 | operationId: result.OperationId, |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | if (request.method === "POST" && pathname === "/deregister") { |
| 171 | const body = (yield* request.json) as unknown as { |
| 172 | instanceId: string; |
| 173 | }; |
| 174 | const result = yield* deregisterInstance({ |
| 175 | InstanceId: body.instanceId, |
| 176 | }); |
| 177 | return yield* HttpServerResponse.json({ |
| 178 | operationId: result.OperationId, |
| 179 | }); |
| 180 | } |
| 181 | |
| 182 | if (request.method === "POST" && pathname === "/custom/register") { |
| 183 | const body = (yield* request.json) as unknown as { |
| 184 | instanceId: string; |
| 185 | attributes: Record<string, string>; |
| 186 | }; |
| 187 | const result = yield* registerCustomInstance({ |
| 188 | InstanceId: body.instanceId, |
| 189 | Attributes: body.attributes, |
| 190 | }); |
| 191 | return yield* HttpServerResponse.json({ |
| 192 | operationId: result.OperationId, |
| 193 | }); |
| 194 | } |
| 195 | |
| 196 | if (request.method === "POST" && pathname === "/custom/deregister") { |
| 197 | const body = (yield* request.json) as unknown as { |
| 198 | instanceId: string; |
| 199 | }; |
| 200 | const result = yield* deregisterCustomInstance({ |
| 201 | InstanceId: body.instanceId, |
| 202 | }); |
| 203 | return yield* HttpServerResponse.json({ |
| 204 | operationId: result.OperationId, |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | if (request.method === "POST" && pathname === "/custom/health") { |
| 209 | const body = (yield* request.json) as unknown as { |
| 210 | instanceId: string; |
| 211 | status: "HEALTHY" | "UNHEALTHY"; |
| 212 | }; |
| 213 | yield* updateCustomHealth({ |
| 214 | InstanceId: body.instanceId, |
| 215 | Status: body.status, |
| 216 | }); |
| 217 | return yield* HttpServerResponse.json({ ok: true }); |
| 218 | } |
| 219 | |
| 220 | if (request.method === "GET" && pathname === "/custom/health-status") { |
| 221 | const result = yield* getCustomHealthStatus(); |
| 222 | return yield* HttpServerResponse.json({ |
| 223 | status: result.Status ?? {}, |
| 224 | }); |
| 225 | } |
| 226 | |
| 227 | return yield* HttpServerResponse.json({ error: "Not found" }, { status: 404 }); |
| 228 | }).pipe(Effect.orDie), |
| 229 | }; |
| 230 | }).pipe( |
| 231 | Effect.provide( |
| 232 | Layer.mergeAll( |
| 233 | AWS.CloudMap.DiscoverInstancesHttp, |
| 234 | AWS.CloudMap.DiscoverInstancesRevisionHttp, |
| 235 | AWS.CloudMap.RegisterInstanceHttp, |
| 236 | AWS.CloudMap.DeregisterInstanceHttp, |
| 237 | AWS.CloudMap.GetInstanceHttp, |
| 238 | AWS.CloudMap.ListInstancesHttp, |
| 239 | AWS.CloudMap.GetServiceAttributesHttp, |
| 240 | AWS.CloudMap.GetOperationHttp, |
| 241 | AWS.CloudMap.UpdateInstanceCustomHealthStatusHttp, |
| 242 | AWS.CloudMap.GetInstancesHealthStatusHttp, |
| 243 | ), |
| 244 | ), |
| 245 | ), |
| 246 | ); |