@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 70 lines 2.4 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as RedshiftServerless from "@alchemy.run/aws/RedshiftServerless";
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 Redacted from "effect/Redacted";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "connect-handler.ts");
11
12 export class ServerlessConnectFunction extends Lambda.Function<Lambda.Function>()(
13 "ServerlessConnectFunction",
14 ) {}
15
16 export default ServerlessConnectFunction.make(
17 {
18 main,
19 functionUrl: true,
20 timeout: Duration.seconds(60),
21 },
22 Effect.gen(function* () {
23 const namespace = yield* RedshiftServerless.Namespace("ConnectNamespace", {
24 namespaceName: "alchemy-test-rsconn-ns",
25 dbName: "dev",
26 adminUsername: "alchemyadmin",
27 manageAdminPassword: true,
28 });
29 const workgroup = yield* RedshiftServerless.Workgroup("ConnectWorkgroup", {
30 workgroupName: "alchemy-test-rsconn-wg",
31 namespaceName: namespace.namespaceName,
32 baseCapacity: 8,
33 publiclyAccessible: false,
34 });
35
36 const connect = yield* RedshiftServerless.Connect(workgroup, {
37 database: "dev",
38 });
39
40 return {
41 fetch: Effect.gen(function* () {
42 const request = yield* HttpServerRequest;
43 const url = new URL(request.originalUrl);
44 const pathname = url.pathname;
45
46 if (request.method === "GET" && pathname === "/info") {
47 // Mints fresh temporary credentials via
48 // redshift-serverless:GetCredentials on every request.
49 const info = yield* connect;
50 return yield* HttpServerResponse.json({
51 host: info.host,
52 port: info.port,
53 database: info.database,
54 username: info.username,
55 hasPassword: info.password !== undefined,
56 ssl: info.ssl,
57 urlScheme: Redacted.value(info.url).split("://")[0],
58 expiresInFuture:
59 info.expiration !== undefined && info.expiration.getTime() > Date.now(),
60 });
61 }
62
63 return yield* HttpServerResponse.json(
64 { error: "Not found", method: request.method, pathname },
65 { status: 404 },
66 );
67 }).pipe(Effect.orDie),
68 };
69 }).pipe(Effect.provide(RedshiftServerless.ConnectHttp)),
70 );