| 1 | import { bindFunction } from "@/Railway/Bind.ts"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import { Api } from "./rpc-api-tag.ts"; |
| 6 | import Query from "./rpc-query.ts"; |
| 7 | import { Partition, Site } from "./rpc-shared.ts"; |
| 8 | |
| 9 | export { Api }; |
| 10 | |
| 11 | /** |
| 12 | * Tagged Service that hosts `ping` and binds the tagged {@link Query} |
| 13 | * Function. `GET /` is local; `GET /hello` calls `query.greet("sam")`. |
| 14 | * |
| 15 | * Default-export the `.make()` Layer: the Docker bootstrap loads |
| 16 | * `default`, and a Tag class never registers `fetch`/`run`. |
| 17 | */ |
| 18 | export const ApiLive = Api.make( |
| 19 | { |
| 20 | project: Site, |
| 21 | environment: Partition, |
| 22 | main: import.meta.url, |
| 23 | port: 3000, |
| 24 | }, |
| 25 | Effect.gen(function* () { |
| 26 | const query = yield* bindFunction(Query); |
| 27 | return { |
| 28 | ping: () => Effect.succeed("pong"), |
| 29 | fetch: Effect.gen(function* () { |
| 30 | const request = yield* HttpServerRequest; |
| 31 | if (request.url.includes("/hello")) { |
| 32 | return HttpServerResponse.text(yield* query.greet("sam")); |
| 33 | } |
| 34 | return HttpServerResponse.text("api"); |
| 35 | }), |
| 36 | }; |
| 37 | }), |
| 38 | ); |
| 39 | |
| 40 | export default ApiLive; |