@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 56 lines 2.01 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import Quotes from "./Quotes.ts";
6
7 /**
8 * The public front door. It forwards `GET /quote` to the private
9 * {@link Quotes} service.
10 *
11 * `GCP.Run.InvokeService(quotes)` does both halves of the wiring: at
12 * deploy time it grants `roles/run.invoker` on the Quotes service (and
13 * only that service) to the Gateway's runtime service account and binds
14 * Quotes' URL into the Gateway; at runtime it mints a Google-signed ID
15 * token for that URL from the metadata server and sends it as
16 * `Authorization: Bearer`.
17 */
18 export default class Gateway extends GCP.Function<Gateway>()(
19 "Gateway",
20 {
21 main: import.meta.url,
22 invokerIamDisabled: true,
23 },
24 Effect.gen(function* () {
25 const quotes = yield* GCP.Run.InvokeService(Quotes);
26
27 return {
28 fetch: Effect.gen(function* () {
29 const request = yield* HttpServerRequest;
30 const url = new URL(request.originalUrl, "http://localhost");
31
32 if (request.method === "GET" && url.pathname === "/") {
33 return HttpServerResponse.text("ok");
34 }
35
36 if (request.method === "GET" && url.pathname === "/quote") {
37 const response = yield* quotes.fetch(`/quote${url.search}`).pipe(
38 // Treat an unreachable backend as a 502, not a crash.
39 Effect.catchTag("GCP.Run.InvokeServiceError", (error) =>
40 Effect.succeed({
41 status: 502,
42 text: Effect.succeed(JSON.stringify({ error: error.message })),
43 }),
44 ),
45 );
46 return HttpServerResponse.text(yield* response.text, {
47 status: response.status,
48 contentType: "application/json",
49 });
50 }
51
52 return yield* HttpServerResponse.json({ error: "not found" }, { status: 404 });
53 }),
54 };
55 }).pipe(Effect.provide(GCP.Run.InvokeServiceHttp)),
56 ) {}