@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 96 lines 3.35 KB
Raw
1 import * as DLM from "@alchemy.run/aws/DLM";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import * as Stream from "effect/Stream";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "handler.ts");
12
13 export class DlmTestFunction extends Lambda.Function<Lambda.Function>()("DlmTestFunction") {}
14
15 export default DlmTestFunction.make(
16 {
17 main,
18 functionUrl: true,
19 timeout: Duration.seconds(30),
20 },
21 Effect.gen(function* () {
22 // The lifecycle policy the policy-scoped binding is bound to. DISABLED
23 // and targeting a tag no volume carries, so it never creates snapshots.
24 const policy = yield* DLM.LifecyclePolicy("BindingPolicy", {
25 description: "alchemy dlm bindings fixture policy",
26 state: "DISABLED",
27 policyDetails: {
28 resourceTypes: ["VOLUME"],
29 targetTags: { AlchemyDlmBindings: "true" },
30 schedules: [
31 {
32 name: "Daily",
33 createRule: { interval: 24, intervalUnit: "HOURS" },
34 retainRule: { count: 1 },
35 },
36 ],
37 },
38 });
39
40 // Event source: subscribe the host to DLM policy state-change events.
41 // The deploy proves the EventBridge rule + invoke permission wiring.
42 yield* DLM.consumePolicyEvents({ kinds: ["state-change"] }, (events) =>
43 Stream.runForEach(events, (event) =>
44 Effect.log(`dlm state change: ${event.detail.policy_id} -> ${event.detail.state}`),
45 ),
46 );
47
48 const getLifecyclePolicy = yield* DLM.GetLifecyclePolicy(policy);
49 const getLifecyclePolicies = yield* DLM.GetLifecyclePolicies();
50
51 const bound = { getLifecyclePolicy, getLifecyclePolicies };
52
53 return {
54 fetch: Effect.gen(function* () {
55 const request = yield* HttpServerRequest;
56 const url = new URL(request.originalUrl);
57 const pathname = url.pathname;
58
59 if (request.method === "GET" && pathname === "/bindings") {
60 return yield* HttpServerResponse.json({
61 bound: Object.keys(bound),
62 });
63 }
64
65 // Policy-scoped read: the PolicyId is injected from the binding.
66 if (request.method === "GET" && pathname === "/policy") {
67 const { Policy } = yield* getLifecyclePolicy();
68 return yield* HttpServerResponse.json({
69 policyId: Policy?.PolicyId,
70 state: Policy?.State,
71 policyType: Policy?.PolicyDetails?.PolicyType,
72 });
73 }
74
75 // Account-level list, filtered down to the fixture's policy state.
76 if (request.method === "GET" && pathname === "/policies") {
77 const { Policies } = yield* getLifecyclePolicies({
78 State: "DISABLED",
79 });
80 return yield* HttpServerResponse.json({
81 ids: (Policies ?? []).map((summary) => summary.PolicyId),
82 });
83 }
84
85 return yield* HttpServerResponse.json(
86 { error: "Not found", method: request.method, pathname },
87 { status: 404 },
88 );
89 }).pipe(Effect.orDie),
90 };
91 }).pipe(
92 Effect.provide(
93 Layer.mergeAll(Lambda.EventSource, DLM.GetLifecyclePolicyHttp, DLM.GetLifecyclePoliciesHttp),
94 ),
95 ),
96 );