| 1 | import { bindService } from "@/Railway/Bind.ts"; |
| 2 | import { Function } from "@/Railway/Function.ts"; |
| 3 | import { enableRailwayRpc } from "@/Railway/rpc-server.ts"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import { Api } from "./rpc-api-tag.ts"; |
| 8 | import { 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 | */ |
| 15 | export 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 | ) {} |