The best way to control Claude Code, Codex and more from any computer or phone

main
ts 82 lines 2.5 KB
Raw
1 import * as Railway from "@alchemy.run/railway";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import * as Stream from "effect/Stream";
6 import { Partition, Site } from "./suite-env.ts";
7
8 export { Site };
9
10 export const BUCKET_PORT = 3000;
11 export const OBJECT_KEY = "alchemy-marker.txt";
12 export const OBJECT_BODY = "hello-from-railway";
13
14 export const Data = Railway.Bucket("Data", {
15 project: Site,
16 environment: Partition,
17 });
18
19 /**
20 * HTTP Service that puts and gets an object on a Railway bucket via
21 * the S3 API.
22 */
23 export default class BucketApi extends Railway.Service<BucketApi>()(
24 "BucketApi",
25 Effect.gen(function* () {
26 const bucket = yield* Data;
27 return {
28 project: Site,
29 environment: Partition,
30 main: import.meta.url,
31 port: BUCKET_PORT,
32 env: {
33 BUCKET_NAME: bucket.s3BucketName,
34 AWS_ACCESS_KEY_ID: bucket.accessKeyId,
35 AWS_SECRET_ACCESS_KEY: bucket.secretAccessKey,
36 AWS_ENDPOINT_URL_S3: bucket.endpoint,
37 AWS_ENDPOINT_URL: bucket.endpoint,
38 AWS_REGION: bucket.s3Region,
39 },
40 };
41 }),
42 Effect.gen(function* () {
43 const putObject = yield* Railway.PutObject(Data);
44 const getObject = yield* Railway.GetObject(Data);
45
46 return {
47 fetch: Effect.gen(function* () {
48 const request = yield* HttpServerRequest;
49 const path = new URL(request.url, "http://service").pathname;
50
51 if (path === "/health") {
52 return yield* HttpServerResponse.json({ ok: true });
53 }
54
55 if (path === "/put") {
56 yield* putObject({
57 Key: OBJECT_KEY,
58 Body: OBJECT_BODY,
59 ContentType: "text/plain",
60 });
61 return yield* HttpServerResponse.json({ ok: true, key: OBJECT_KEY });
62 }
63
64 if (path === "/get") {
65 const obj = yield* getObject({ Key: OBJECT_KEY });
66 const text =
67 obj.Body === undefined ? "" : yield* Stream.mkString(Stream.decodeText(obj.Body));
68 return yield* HttpServerResponse.json({
69 ok: text === OBJECT_BODY,
70 text,
71 });
72 }
73
74 return yield* HttpServerResponse.json({ ok: false }, { status: 404 });
75 }).pipe(
76 Effect.catch((error) =>
77 HttpServerResponse.json({ ok: false, error: String(error) }, { status: 500 }),
78 ),
79 ),
80 };
81 }).pipe(Effect.provide([Railway.PutObjectHttp, Railway.GetObjectHttp])),
82 ) {}