1import { bindService } from "@/Railway/Bind.ts";
2import { Function } from "@/Railway/Function.ts";
3import { enableRailwayRpc } from "@/Railway/rpc-server.ts";
4import * as Effect from "effect/Effect";
5import { HttpServerRequest } from "effect/http/HttpServerRequest";
6import * as HttpServerResponse from "effect/http/HttpServerResponse";
7import { Api } from "./rpc-api-tag.ts";
8import { Partition, Site } from "./rpc-shared.ts";
9
10/**
11 * Tagged Function that hosts `greet` and binds the tagged {@link Api}
12 * Service. `GET /` is local so the public probe does not deadlock on
13 * private-mesh RPC; `GET /pong` calls `api.ping()`.
14 */
15export default class Query extends Function<Query>()(
16 "Query",
17 { project: Site, environment: Partition, main: import.meta.url },
18 Effect.gen(function* () {
19 enableRailwayRpc();
20 const api = yield* bindService(Api);
21 return {
22 greet: (name: string) => Effect.succeed(`hello ${name}`),
23 fetch: Effect.gen(function* () {
24 const request = yield* HttpServerRequest;
25 if (request.url.includes("/pong")) {
26 return HttpServerResponse.text(yield* api.ping());
27 }
28 return HttpServerResponse.text("query");
29 }),
30 };
31 }),
32) {}