The best way to control Claude Code, Codex and more from any computer or phone

main
ts 131 lines 4.72 KB
Raw
1 import * as AppRunner from "@alchemy.run/aws/AppRunner";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Cause from "effect/Cause";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import { HttpServerRequest } from "effect/http/HttpServerRequest";
7 import * as HttpServerResponse from "effect/http/HttpServerResponse";
8 import * as Layer from "effect/Layer";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "handler.ts");
12
13 /**
14 * Bindings fixture: a Lambda that manages an App Runner service through the
15 * ListOperations / PauseService / ResumeService / StartDeployment bindings.
16 * The service itself is the cheap public-ECR hello image (no access role, no
17 * Docker build).
18 */
19 export class AppRunnerTestFunction extends Lambda.Function<Lambda.Function>()(
20 "AppRunnerTestFunction",
21 ) {}
22
23 export default AppRunnerTestFunction.make(
24 {
25 main,
26 functionUrl: true,
27 // Pause/resume calls fan out SDK calls; AWS's 3s default intermittently
28 // times out on a cold start.
29 timeout: Duration.seconds(30),
30 },
31 Effect.gen(function* () {
32 const service = yield* AppRunner.Service("BindingsService", {
33 serviceName: "alchemy-test-apprunner-bind",
34 imageRepository: {
35 imageIdentifier: "public.ecr.aws/aws-containers/hello-app-runner:latest",
36 imageRepositoryType: "ECR_PUBLIC",
37 port: "8000",
38 },
39 instanceConfiguration: { cpu: "256", memory: "512" },
40 });
41
42 const listOperations = yield* AppRunner.ListOperations(service);
43 const pauseService = yield* AppRunner.PauseService(service);
44 const resumeService = yield* AppRunner.ResumeService(service);
45 const startDeployment = yield* AppRunner.StartDeployment(service);
46 const describeCustomDomains = yield* AppRunner.DescribeCustomDomains(service);
47
48 return {
49 fetch: Effect.gen(function* () {
50 const request = yield* HttpServerRequest;
51 const pathname = new URL(request.originalUrl).pathname;
52
53 if (request.method === "GET" && pathname === "/operations") {
54 const result = yield* listOperations({ MaxResults: 20 });
55 return yield* HttpServerResponse.json({
56 operations: (result.OperationSummaryList ?? []).map((op) => ({
57 id: op.Id,
58 type: op.Type,
59 status: op.Status,
60 })),
61 });
62 }
63
64 if (request.method === "POST" && pathname === "/pause") {
65 const result = yield* pauseService();
66 return yield* HttpServerResponse.json({
67 serviceArn: result.Service.ServiceArn,
68 status: result.Service.Status,
69 operationId: result.OperationId,
70 });
71 }
72
73 if (request.method === "POST" && pathname === "/resume") {
74 const result = yield* resumeService();
75 return yield* HttpServerResponse.json({
76 serviceArn: result.Service.ServiceArn,
77 status: result.Service.Status,
78 operationId: result.OperationId,
79 });
80 }
81
82 if (request.method === "POST" && pathname === "/deploy") {
83 const result = yield* startDeployment();
84 return yield* HttpServerResponse.json({
85 operationId: result.OperationId,
86 });
87 }
88
89 if (request.method === "GET" && pathname === "/custom-domains") {
90 const result = yield* describeCustomDomains();
91 return yield* HttpServerResponse.json({
92 dnsTarget: result.DNSTarget,
93 customDomains: (result.CustomDomains ?? []).map((domain) => ({
94 domainName: domain.DomainName,
95 status: domain.Status,
96 })),
97 });
98 }
99
100 if (request.method === "GET" && pathname === "/ping") {
101 return yield* HttpServerResponse.json({ ok: true });
102 }
103
104 return yield* HttpServerResponse.json(
105 { error: "Not found", method: request.method, pathname },
106 { status: 404 },
107 );
108 }).pipe(
109 // Render the failure into the response instead of dying: a binding
110 // that is rejected (an IAM grant that has not propagated yet, an
111 // InvalidStateException) otherwise reaches the test as an opaque
112 // Lambda 502, and the assertion blows up on a missing field rather
113 // than saying what App Runner actually refused.
114 Effect.catchCause((cause) =>
115 HttpServerResponse.json({ error: Cause.pretty(cause) }, { status: 500 }),
116 ),
117 Effect.orDie,
118 ),
119 };
120 }).pipe(
121 Effect.provide(
122 Layer.mergeAll(
123 AppRunner.DescribeCustomDomainsHttp,
124 AppRunner.ListOperationsHttp,
125 AppRunner.PauseServiceHttp,
126 AppRunner.ResumeServiceHttp,
127 AppRunner.StartDeploymentHttp,
128 ),
129 ),
130 ),
131 );