@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 71 lines 2.45 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Redshift from "@alchemy.run/aws/Redshift";
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 RedshiftConnectFunction extends Lambda.Function<Lambda.Function>()(
13 "RedshiftConnectFunction",
14 ) {}
15
16 const summarize = (info: Redshift.ClusterConnectionInfo) => ({
17 host: info.host,
18 port: info.port,
19 database: info.database,
20 username: info.username,
21 hasPassword: info.password !== undefined,
22 ssl: info.ssl,
23 urlScheme: Redacted.value(info.url).split("://")[0],
24 expiresInFuture: info.expiration !== undefined && info.expiration.getTime() > Date.now(),
25 });
26
27 export default RedshiftConnectFunction.make(
28 {
29 main,
30 functionUrl: true,
31 timeout: Duration.seconds(60),
32 },
33 Effect.gen(function* () {
34 const cluster = yield* Redshift.Cluster("ConnectCluster", {
35 clusterIdentifier: "alchemy-test-redshift-connect",
36 manageMasterPassword: true,
37 publiclyAccessible: false,
38 });
39
40 // Default strategy: the database user is mapped 1:1 to the Lambda's IAM
41 // identity via redshift:GetClusterCredentialsWithIAM.
42 const connectIam = yield* Redshift.Connect(cluster);
43 // Named-user strategy: redshift:GetClusterCredentials with AutoCreate.
44 const connectDbUser = yield* Redshift.Connect(cluster, {
45 dbUser: "alchemy_etl",
46 autoCreate: true,
47 });
48
49 return {
50 fetch: Effect.gen(function* () {
51 const request = yield* HttpServerRequest;
52 const url = new URL(request.originalUrl);
53 const pathname = url.pathname;
54
55 if (request.method === "GET" && pathname === "/info") {
56 const info = yield* connectIam;
57 return yield* HttpServerResponse.json(summarize(info));
58 }
59 if (request.method === "GET" && pathname === "/info-dbuser") {
60 const info = yield* connectDbUser;
61 return yield* HttpServerResponse.json(summarize(info));
62 }
63
64 return yield* HttpServerResponse.json(
65 { error: "Not found", method: request.method, pathname },
66 { status: 404 },
67 );
68 }).pipe(Effect.orDie),
69 };
70 }).pipe(Effect.provide(Redshift.ConnectHttp)),
71 );