@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 80 lines 2.8 KB
Raw
1 import * as Fly from "@alchemy.run/fly";
2 import * as Cause from "effect/Cause";
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 Redacted from "effect/Redacted";
7
8 export const CERT_API_PORT = 3000;
9
10 export const CertSite = Fly.App("CertSite", { enableSubdomains: true });
11
12 export const CertIp = Fly.IpAssignment("CertShared", {
13 app: CertSite,
14 type: "shared_v4",
15 });
16
17 /**
18 * HTTP Service exercising {@link Fly.WriteCertificates}: one route per
19 * operation, the hostname from the query string, PEMs in the JSON body.
20 */
21 export default class CertificatesApi extends Fly.Service<CertificatesApi>()(
22 "CertificatesApi",
23 {
24 app: CertSite,
25 main: import.meta.url,
26 region: "iad",
27 port: CERT_API_PORT,
28 guest: { cpuKind: "shared", cpus: 1, memoryMb: 256 },
29 },
30 Effect.gen(function* () {
31 const certs = yield* Fly.WriteCertificates(CertSite);
32
33 return {
34 fetch: Effect.gen(function* () {
35 const request = yield* HttpServerRequest;
36 const url = new URL(request.url, "http://service");
37 const hostname = url.searchParams.get("hostname") ?? "";
38
39 // `suspend` turns a synchronous throw inside the client call into a
40 // defect the cause handler below can report.
41 const respond = <A, E, R>(make: () => Effect.Effect<A, E, R>) =>
42 Effect.suspend(make).pipe(
43 Effect.flatMap((value) => HttpServerResponse.json({ ok: true, value: value ?? null })),
44 Effect.catchCause((cause) =>
45 HttpServerResponse.json({ ok: false, error: Cause.pretty(cause) }, { status: 500 }),
46 ),
47 );
48
49 switch (url.pathname) {
50 case "/health":
51 return yield* HttpServerResponse.json({ ok: true, version: 2 });
52 case "/request":
53 return yield* respond(() => certs.request(hostname));
54 case "/upload": {
55 const body = (yield* request.json) as {
56 hostname: string;
57 fullchain: string;
58 privateKey: string;
59 };
60 return yield* respond(() =>
61 certs.upload({
62 hostname: body.hostname,
63 fullchain: body.fullchain,
64 privateKey: Redacted.make(body.privateKey),
65 }),
66 );
67 }
68 case "/check":
69 return yield* respond(() => certs.check(hostname));
70 case "/get":
71 return yield* respond(() => certs.get(hostname));
72 case "/remove":
73 return yield* respond(() => certs.remove(hostname));
74 default:
75 return HttpServerResponse.text("not found", { status: 404 });
76 }
77 }),
78 };
79 }).pipe(Effect.provide(Fly.WriteCertificatesHttp)),
80 ) {}