@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.24 KB
Raw
1 import * as Drizzle from "@alchemy.run/drizzle/Postgres";
2 import * as Fly from "@alchemy.run/fly";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Result from "effect/Result";
7
8 export const POSTGRES_PORT = 3000;
9
10 export const MpgSite = Fly.App("MpgSite", {
11 enableSubdomains: true,
12 });
13
14 export const Db = Fly.Postgres("Db", {
15 region: "iad",
16 plan: "basic",
17 volumeSizeGb: 10,
18 });
19
20 export const MpgIp = Fly.IpAssignment("Shared", {
21 app: MpgSite,
22 type: "shared_v4",
23 });
24
25 /**
26 * HTTP Service that binds Managed Postgres via {@link Fly.ConnectPostgres}
27 * and answers SELECT 1. Never returns the connection string.
28 */
29 export default class PostgresApi extends Fly.Service<PostgresApi>()(
30 "PostgresApi",
31 {
32 app: MpgSite,
33 main: import.meta.url,
34 region: "iad",
35 port: POSTGRES_PORT,
36 guest: { cpuKind: "shared", cpus: 1, memoryMb: 512 },
37 },
38 Effect.gen(function* () {
39 const conn = yield* Fly.ConnectPostgres(Db);
40 const db = yield* Drizzle.Postgres(conn.connectionString);
41 const direct = yield* Drizzle.Postgres(conn.directConnectionString);
42
43 return {
44 fetch: Effect.gen(function* () {
45 const request = yield* HttpServerRequest;
46 const path = new URL(request.url, "http://service").pathname;
47 if (path === "/ping") {
48 return yield* HttpServerResponse.json({ ok: true });
49 }
50 const client = path === "/direct" ? direct : db;
51 const result = yield* Effect.result(client.execute("select 1 as ok"));
52 if (Result.isFailure(result)) {
53 const error = result.failure;
54 return yield* HttpServerResponse.json(
55 {
56 ok: false,
57 error: error instanceof Error ? `${error.name}: ${error.message}` : String(error),
58 },
59 { status: 500 },
60 );
61 }
62 const rows = result.success;
63 if (path === "/health" || path === "/direct" || path === "/") {
64 return yield* HttpServerResponse.json({ rows });
65 }
66 return yield* HttpServerResponse.json({ rows }, { status: 404 });
67 }),
68 };
69 }).pipe(Effect.provide(Fly.ConnectPostgresHttp)),
70 ) {}