@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, 04:45 UTC

main
ts 90 lines 2.64 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { SQSQueueEventSource } from "@alchemy.run/aws/Server/SQSQueueEventSource";
3 import * as Effect from "effect/Effect";
4 import * as HttpServerRequest from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import { Network, NetworkLive } from "./Network.ts";
8
9 export default class Server extends AWS.EC2.Instance<Server>()(
10 "ServerInstance",
11 Effect.gen(function* () {
12 const network = yield* Network;
13
14 return {
15 main: import.meta.url,
16 imageId: AWS.EC2.amazonLinux(),
17 instanceType: "t3.small",
18 subnetId: network.publicSubnetIds[0],
19 securityGroupIds: [network.appSecurityGroupId],
20 associatePublicIpAddress: true,
21 port: 3000,
22 };
23 }),
24 Effect.gen(function* () {
25 const queue = yield* AWS.SQS.Queue("JobsQueue", {
26 receiveMessageWaitTime: "20 seconds",
27 visibilityTimeout: "60 seconds",
28 });
29
30 yield* AWS.SQS.consumeQueueMessages(
31 queue,
32 Effect.fn(function* (event) {
33 for (const record of event.Records) {
34 yield* Effect.logInfo(record);
35 }
36 }),
37 );
38
39 const sendMessage = yield* AWS.SQS.SendMessage(queue);
40
41 return {
42 fetch: Effect.gen(function* () {
43 const request = yield* HttpServerRequest.HttpServerRequest;
44 const url = new URL(request.originalUrl);
45
46 if (request.method === "GET" && url.pathname === "/") {
47 return yield* HttpServerResponse.json({
48 ok: true,
49 routes: ["GET /", "GET /enqueue?message=hello"],
50 });
51 }
52
53 if (request.method === "GET" && url.pathname === "/enqueue") {
54 const message = url.searchParams.get("message") ?? "hello from EC2";
55 const body = JSON.stringify({
56 message,
57 enqueuedAt: new Date().toISOString(),
58 });
59
60 const result = yield* sendMessage({
61 MessageBody: body,
62 });
63
64 return yield* HttpServerResponse.json({
65 ok: true,
66 message,
67 messageId: result.MessageId,
68 });
69 }
70
71 return HttpServerResponse.text("Not found", { status: 404 });
72 }).pipe(
73 Effect.catch(() =>
74 Effect.succeed(HttpServerResponse.text("Internal server error", { status: 500 })),
75 ),
76 ),
77 };
78 }).pipe(
79 Effect.provide(
80 Layer.provideMerge(
81 Layer.mergeAll(NetworkLive, SQSQueueEventSource),
82 Layer.mergeAll(
83 AWS.SQS.DeleteMessageBatchHttp,
84 AWS.SQS.ReceiveMessageHttp,
85 AWS.SQS.SendMessageHttp,
86 ),
87 ),
88 ),
89 ),
90 ) {}