@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 179 lines 6.54 KB
Raw
1 import * as AppConfig from "@alchemy.run/aws/AppConfig";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Output from "alchemy/Output";
4 import * as Cause from "effect/Cause";
5 import * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import { HttpServerRequest } from "effect/http/HttpServerRequest";
8 import * as HttpServerResponse from "effect/http/HttpServerResponse";
9 import * as Layer from "effect/Layer";
10 import path from "pathe";
11
12 const main = path.resolve(import.meta.dirname, "handler.ts");
13
14 const CONFIG = { featureX: true, limit: 42 } as const;
15
16 export class AppConfigTestFunction extends Lambda.Function<Lambda.Function>()(
17 "AppConfigTestFunction",
18 ) {}
19
20 export default AppConfigTestFunction.make(
21 {
22 main,
23 functionUrl: true,
24 // Starting a configuration session + fetching config fans out two SDK
25 // calls; AWS's 3s default intermittently times out on a cold start.
26 timeout: Duration.seconds(30),
27 },
28 Effect.gen(function* () {
29 const app = yield* AppConfig.Application("App", {});
30 const env = yield* AppConfig.Environment("Env", {
31 applicationId: app.applicationId,
32 });
33 const profile = yield* AppConfig.ConfigurationProfile("Profile", {
34 applicationId: app.applicationId,
35 locationUri: "hosted",
36 });
37 const version = yield* AppConfig.HostedConfigurationVersion("V1", {
38 applicationId: app.applicationId,
39 configurationProfileId: profile.configurationProfileId,
40 content: JSON.stringify(CONFIG),
41 contentType: "application/json",
42 });
43 const strategy = yield* AppConfig.DeploymentStrategy("AllAtOnce", {
44 deploymentDuration: 0,
45 growthFactor: 100,
46 finalBakeTime: 0,
47 replicateTo: "NONE",
48 });
49 // A deliberately slow strategy so a rollout can be stopped mid-flight.
50 const slowStrategy = yield* AppConfig.DeploymentStrategy("Slow", {
51 deploymentDuration: "20 minutes",
52 growthFactor: 5,
53 growthType: "LINEAR",
54 finalBakeTime: 0,
55 replicateTo: "NONE",
56 });
57 // Deploy the version to the environment so the data plane can serve it.
58 yield* AppConfig.Deployment("Deploy", {
59 applicationId: app.applicationId,
60 environmentId: env.environmentId,
61 deploymentStrategyId: strategy.deploymentStrategyId,
62 configurationProfileId: profile.configurationProfileId,
63 configurationVersion: Output.interpolate`${version.versionNumber}`,
64 });
65
66 const getConfig = yield* AppConfig.GetConfiguration(app, env, profile);
67 const createVersion = yield* AppConfig.CreateHostedConfigurationVersion(app, profile);
68 const startDeployment = yield* AppConfig.StartDeployment(app, env, profile, strategy);
69 const startSlowDeployment = yield* AppConfig.StartDeployment(app, env, profile, slowStrategy);
70 const getDeployment = yield* AppConfig.GetDeployment(app, env);
71 const stopDeployment = yield* AppConfig.StopDeployment(app, env);
72 const validateConfiguration = yield* AppConfig.ValidateConfiguration(app, profile);
73
74 return {
75 fetch: Effect.gen(function* () {
76 const request = yield* HttpServerRequest;
77 const pathname = new URL(request.originalUrl).pathname;
78
79 if (request.method === "GET" && pathname === "/config") {
80 const result = yield* getConfig();
81 return yield* HttpServerResponse.json({
82 content: result.content,
83 contentType: result.contentType,
84 });
85 }
86
87 if (request.method === "POST" && pathname === "/version") {
88 const body = (yield* request.json) as unknown as {
89 content: string;
90 };
91 const created = yield* createVersion({
92 Content: body.content,
93 ContentType: "application/json",
94 });
95 return yield* HttpServerResponse.json({
96 versionNumber: created.VersionNumber,
97 });
98 }
99
100 if (request.method === "POST" && pathname === "/deploy") {
101 const body = (yield* request.json) as unknown as {
102 version: string;
103 slow?: boolean;
104 };
105 const started = yield* (body.slow ? startSlowDeployment : startDeployment)({
106 ConfigurationVersion: body.version,
107 });
108 return yield* HttpServerResponse.json({
109 deploymentNumber: started.DeploymentNumber,
110 state: started.State,
111 });
112 }
113
114 if (request.method === "GET" && pathname === "/deployment") {
115 const number = Number(new URL(request.originalUrl).searchParams.get("number"));
116 const deployment = yield* getDeployment({
117 DeploymentNumber: number,
118 });
119 return yield* HttpServerResponse.json({
120 state: deployment.State,
121 percentageComplete: deployment.PercentageComplete,
122 configurationVersion: deployment.ConfigurationVersion,
123 });
124 }
125
126 if (request.method === "POST" && pathname === "/stop") {
127 const body = (yield* request.json) as unknown as {
128 number: number;
129 };
130 const stopped = yield* stopDeployment({
131 DeploymentNumber: body.number,
132 });
133 return yield* HttpServerResponse.json({ state: stopped.State });
134 }
135
136 if (request.method === "POST" && pathname === "/validate") {
137 const body = (yield* request.json) as unknown as {
138 version: string;
139 };
140 yield* validateConfiguration({
141 ConfigurationVersion: body.version,
142 });
143 return yield* HttpServerResponse.json({ valid: true });
144 }
145
146 if (request.method === "GET" && pathname === "/ping") {
147 return yield* HttpServerResponse.json({ ok: true });
148 }
149
150 return yield* HttpServerResponse.json(
151 { error: "Not found", method: request.method, pathname },
152 { status: 404 },
153 );
154 }).pipe(
155 // Surface failures as a 500 whose body carries the cause so the
156 // test's transient-retry error logs show WHAT failed in the Lambda.
157 Effect.catchCause((cause) =>
158 Effect.succeed(
159 HttpServerResponse.text(
160 `${Cause.pretty(cause)}\n${JSON.stringify(Cause.squash(cause))}`,
161 { status: 500 },
162 ),
163 ),
164 ),
165 ),
166 };
167 }).pipe(
168 Effect.provide(
169 Layer.mergeAll(
170 AppConfig.GetConfigurationHttp,
171 AppConfig.CreateHostedConfigurationVersionHttp,
172 AppConfig.StartDeploymentHttp,
173 AppConfig.GetDeploymentHttp,
174 AppConfig.StopDeploymentHttp,
175 AppConfig.ValidateConfigurationHttp,
176 ),
177 ),
178 ),
179 );