@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 69 lines 2.21 KB
Raw
1 import * as Fly from "@alchemy.run/fly";
2 import * as Config from "effect/Config";
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 Ref from "effect/Ref";
7
8 export const Site = Fly.App("Site");
9 export class Api extends Fly.Service<Api>()("Api") {}
10
11 export const apiLayer = (version: string) =>
12 Api.make(
13 {
14 app: Site,
15 main: import.meta.url,
16 env: { VERSION: version },
17 deploy: { strategy: "bluegreen", healthTimeout: "30 seconds" },
18 shutdown: { timeout: "30 seconds" },
19 services: [
20 {
21 protocol: "tcp",
22 internalPort: 3000,
23 autostop: "off",
24 ports: [{ port: 80, handlers: ["http"] }],
25 checks: [
26 {
27 type: "http",
28 port: 3000,
29 path: "/health",
30 interval: "2s",
31 timeout: "1s",
32 },
33 ],
34 },
35 ],
36 },
37 Effect.gen(function* () {
38 const active = yield* Ref.make(0);
39 const shutdown = { received: false };
40 if (globalThis.__ALCHEMY_RUNTIME__) {
41 yield* Effect.sync(() =>
42 process.once("SIGTERM", () => {
43 shutdown.received = true;
44 }),
45 );
46 }
47 return {
48 fetch: Effect.gen(function* () {
49 const version = yield* Config.String("VERSION").pipe(Effect.orDie);
50 const request = yield* HttpServerRequest;
51 if (request.url.startsWith("/health")) return HttpServerResponse.text("ok");
52 if (request.url.startsWith("/active"))
53 return HttpServerResponse.text(String(yield* Ref.get(active)));
54 if (request.url.startsWith("/slow")) {
55 yield* Ref.update(active, (count) => count + 1);
56 yield* Effect.sleep("60 seconds").pipe(
57 Effect.ensuring(Ref.update(active, (count) => count - 1)),
58 );
59 return HttpServerResponse.text(
60 `${version}:${shutdown.received ? "drained" : "not-signaled"}`,
61 );
62 }
63 return HttpServerResponse.text(version);
64 }),
65 };
66 }),
67 );
68
69 export default apiLayer("default");