sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ Railway/ fixtures/ rpc-api.ts

import { bindFunction } from "@alchemy.run/railway/Bind";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { Api } from "./rpc-api-tag.ts";
import Query from "./rpc-query.ts";
import { Partition, Site } from "./rpc-shared.ts";

export { Api };

/**
 * Tagged Service that hosts `ping` and binds the tagged {@link Query}
 * Function. `GET /` is local; `GET /hello` calls `query.greet("sam")`.
 *
 * Default-export the `.make()` Layer: the Docker bootstrap loads
 * `default`, and a Tag class never registers `fetch`/`run`.
 */
export const ApiLive = Api.make(
  {
    project: Site,
    environment: Partition,
    main: import.meta.url,
    port: 3000,
  },
  Effect.gen(function* () {
    const query = yield* bindFunction(Query);
    return {
      ping: () => Effect.succeed("pong"),
      fetch: Effect.gen(function* () {
        const request = yield* HttpServerRequest;
        if (request.url.includes("/hello")) {
          return HttpServerResponse.text(yield* query.greet("sam"));
        }
        return HttpServerResponse.text("api");
      }),
    };
  }),
);

export default ApiLive;