@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 113 lines 3.49 KB
Raw
1 import * as Drizzle from "@alchemy.run/drizzle/Postgres";
2 import * as Railway from "@alchemy.run/railway";
3 import * as Config from "effect/Config";
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 Layer from "effect/Layer";
8 import * as Stream from "effect/Stream";
9 import {
10 API_PORT,
11 Cache,
12 Data,
13 Db,
14 Marker,
15 OBJECT_KEY,
16 REDIS_KEY,
17 SECRET_NAME,
18 Site,
19 } from "./shared.ts";
20
21 const runtime = Layer.mergeAll(
22 Railway.ConnectPostgresHttp,
23 Railway.ReadWriteRedisHttp,
24 Railway.PutObjectHttp,
25 Railway.GetObjectHttp,
26 Railway.HeadObjectHttp,
27 Railway.ListObjectsV2Http,
28 Railway.DeleteObjectHttp,
29 );
30
31 /**
32 * HTTP Service. Binds Postgres, Redis, and the Bucket. Reads the
33 * shared {@link Marker} variable from env.
34 */
35 export default class Api extends Railway.Service<Api>()(
36 "Api",
37 {
38 project: Site,
39 main: import.meta.url,
40 port: API_PORT,
41 healthcheck: "/health",
42 env: {
43 [SECRET_NAME]: Railway.ref("shared", SECRET_NAME),
44 },
45 },
46 Effect.gen(function* () {
47 yield* Marker;
48 const conn = yield* Railway.ConnectPostgres(Db);
49 const db = yield* Drizzle.Postgres(conn.connectionString);
50 const cache = yield* Railway.ReadWriteRedis(Cache);
51 const putObject = yield* Railway.PutObject(Data);
52 const getObject = yield* Railway.GetObject(Data);
53 const headObject = yield* Railway.HeadObject(Data);
54 const listObjects = yield* Railway.ListObjectsV2(Data);
55 const deleteObject = yield* Railway.DeleteObject(Data);
56
57 return {
58 fetch: Effect.gen(function* () {
59 const request = yield* HttpServerRequest;
60 const path = new URL(request.url, "http://service").pathname;
61
62 if (path === "/secret") {
63 const value = yield* Config.String(SECRET_NAME).pipe(Effect.orElseSucceed(() => ""));
64 return yield* HttpServerResponse.json({
65 ok: value.length > 0,
66 name: SECRET_NAME,
67 });
68 }
69
70 if (path === "/redis") {
71 yield* cache.set(REDIS_KEY, "hello");
72 const value = yield* cache.get(REDIS_KEY);
73 return yield* HttpServerResponse.json({
74 ok: value === "hello",
75 value,
76 });
77 }
78
79 if (path === "/bucket") {
80 yield* putObject({
81 Key: OBJECT_KEY,
82 Body: "hello",
83 ContentType: "text/plain",
84 });
85 const head = yield* headObject({ Key: OBJECT_KEY });
86 const listed = yield* listObjects({
87 Prefix: OBJECT_KEY,
88 MaxKeys: 10,
89 });
90 const obj = yield* getObject({ Key: OBJECT_KEY });
91 const text =
92 obj.Body === undefined ? "" : yield* Stream.mkString(Stream.decodeText(obj.Body));
93 yield* deleteObject({ Key: OBJECT_KEY });
94 return yield* HttpServerResponse.json({
95 ok: text === "hello",
96 etag: head.ETag,
97 listed: listed.KeyCount,
98 });
99 }
100
101 const rows = yield* db.execute("select 1 as ok");
102 if (path === "/health" || path === "/" || path === "/postgres") {
103 return yield* HttpServerResponse.json({ rows });
104 }
105 return yield* HttpServerResponse.json({ rows }, { status: 404 });
106 }).pipe(
107 Effect.catch((error) =>
108 HttpServerResponse.json({ ok: false, error: String(error) }, { status: 500 }),
109 ),
110 ),
111 };
112 }).pipe(Effect.provide(runtime)),
113 ) {}