@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 103 lines 3.99 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import * as Drizzle from "@alchemy.run/drizzle/Postgres";
3 import { eq, sql } from "drizzle-orm";
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 Redacted from "effect/Redacted";
9 import * as Schema from "effect/Schema";
10 import { bootstrap } from "./bootstrap.ts";
11 import { database } from "./database.ts";
12 import { todos } from "./schema.ts";
13
14 export default class Api extends AWS.Lambda.Function<Api>()(
15 "Api",
16 Effect.gen(function* () {
17 const schemaVersion = yield* bootstrap;
18 return {
19 main: import.meta.url,
20 runtime: "nodejs22.x" as const,
21 functionUrl: { authType: "AWS_IAM" as const },
22 timeout: Duration.seconds(30),
23 env: {
24 SCHEMA_VERSION: schemaVersion,
25 NODE_EXTRA_CA_CERTS: "/var/runtime/ca-cert.pem",
26 },
27 };
28 }),
29 Effect.gen(function* () {
30 const { cluster, subnetIds, lambdaSecurityGroup } = yield* database;
31 const connect = yield* AWS.RDS.Connect(cluster, {
32 auth: "iam",
33 username: "app_iam",
34 database: "app",
35 subnetIds,
36 securityGroupIds: [lambdaSecurityGroup.groupId],
37 });
38 const db = yield* Drizzle.Postgres(
39 connect.pipe(
40 Effect.map((connection) => {
41 const url = new URL(Redacted.value(connection.url));
42 // Connect currently emits no-verify; require full certificate verification.
43 url.searchParams.set("sslmode", "verify-full");
44 return Redacted.make(url.toString());
45 }),
46 ),
47 );
48 return {
49 fetch: Effect.gen(function* () {
50 const request = yield* HttpServerRequest;
51 const url = new URL(request.originalUrl);
52 if (request.method === "GET" && url.pathname === "/health") {
53 const rows = yield* db.execute(sql`SELECT current_user AS username,
54 (SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()) AS tls,
55 has_table_privilege(current_user, 'todos', 'INSERT') AS can_insert,
56 has_schema_privilege(current_user, 'public', 'CREATE') AS can_create`);
57 return yield* HttpServerResponse.json(rows);
58 }
59 if (url.pathname === "/todos" && request.method === "GET") {
60 return yield* HttpServerResponse.json(yield* db.select().from(todos));
61 }
62 if (url.pathname === "/todos" && request.method === "POST") {
63 const input = yield* request.json.pipe(
64 Effect.flatMap(
65 Schema.decodeUnknownEffect(
66 Schema.Struct({
67 id: Schema.String.check(Schema.isUUID()),
68 title: Schema.NonEmptyString,
69 }),
70 ),
71 ),
72 );
73 return yield* HttpServerResponse.json(yield* db.insert(todos).values(input).returning(), {
74 status: 201,
75 });
76 }
77 if (url.pathname.startsWith("/todos/")) {
78 const id = yield* Schema.decodeUnknownEffect(Schema.String.check(Schema.isUUID()))(
79 url.pathname.slice(7),
80 );
81 if (request.method === "PATCH") {
82 const input = yield* request.json.pipe(
83 Effect.flatMap(Schema.decodeUnknownEffect(Schema.Struct({ done: Schema.Boolean }))),
84 );
85 return yield* HttpServerResponse.json(
86 yield* db.update(todos).set(input).where(eq(todos.id, id)).returning(),
87 );
88 }
89 if (request.method === "DELETE") {
90 yield* db.delete(todos).where(eq(todos.id, id));
91 return HttpServerResponse.empty({ status: 204 });
92 }
93 }
94 return HttpServerResponse.text("Not found", { status: 404 });
95 }).pipe(
96 Effect.catchTag("SchemaError", () =>
97 Effect.succeed(HttpServerResponse.text("Invalid request", { status: 400 })),
98 ),
99 Effect.orDie,
100 ),
101 };
102 }).pipe(Effect.provide(AWS.RDS.ConnectHttp)),
103 ) {}