sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ AWS/ ELBv2/ fixtures/ web-handler.ts

import * as Lambda from "@alchemy.run/aws/Lambda";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";

const main = import.meta.url;

/** ALB Lambda target answering the `/web/*` path rule. */
export class WebTargetFunction extends Lambda.Function<Lambda.Function>()("WebTargetFunction") {}

export const WebTargetFunctionLive = WebTargetFunction.make(
  {
    main,
    timeout: Duration.seconds(30),
  },
  Effect.gen(function* () {
    return {
      fetch: Effect.gen(function* () {
        const request = yield* HttpServerRequest;
        const pathname = new URL(request.originalUrl).pathname;
        return yield* HttpServerResponse.json({
          target: "web",
          path: pathname,
        });
      }).pipe(Effect.orDie),
    };
  }),
);

export default WebTargetFunctionLive;