| 1 | import * as Lambda from "@/AWS/Lambda"; |
| 2 | import * as Duration from "effect/Duration"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | |
| 7 | const main = import.meta.url; |
| 8 | |
| 9 | /** ALB Lambda target answering the `/api/*` path rule. */ |
| 10 | export class ApiTargetFunction extends Lambda.Function<Lambda.Function>()( |
| 11 | "ApiTargetFunction", |
| 12 | ) {} |
| 13 | |
| 14 | export const ApiTargetFunctionLive = ApiTargetFunction.make( |
| 15 | { |
| 16 | main, |
| 17 | timeout: Duration.seconds(30), |
| 18 | }, |
| 19 | Effect.gen(function* () { |
| 20 | return { |
| 21 | fetch: Effect.gen(function* () { |
| 22 | const request = yield* HttpServerRequest; |
| 23 | const pathname = new URL(request.originalUrl).pathname; |
| 24 | return yield* HttpServerResponse.json({ |
| 25 | target: "api", |
| 26 | path: pathname, |
| 27 | }); |
| 28 | }).pipe(Effect.orDie), |
| 29 | }; |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | export default ApiTargetFunctionLive; |