| 1 | /** |
| 2 | * @title Using Layer.launch as the application entry point |
| 3 | * |
| 4 | * Use `Layer.launch` to run a long-running Effect program as your process entrypoint. |
| 5 | */ |
| 6 | import { NodeHttpServer, NodeRuntime } from "@effect/platform-node" |
| 7 | import { Effect, Layer } from "effect" |
| 8 | import { HttpRouter, HttpServerResponse } from "effect/http" |
| 9 | import { createServer } from "node:http" |
| 10 | |
| 11 | // Build a tiny HTTP app with a health-check endpoint. |
| 12 | export const HealthRoutes = HttpRouter.use(Effect.fn(function*(router) { |
| 13 | yield* router.add("GET", "/health", Effect.succeed(HttpServerResponse.text("ok"))) |
| 14 | yield* router.add("GET", "/healthz", Effect.succeed(HttpServerResponse.text("ok"))) |
| 15 | })) |
| 16 | |
| 17 | // Turn the routes into a server layer and provide the Node HTTP server backend. |
| 18 | export const HttpServerLive = HttpRouter.serve(HealthRoutes).pipe( |
| 19 | Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 })) |
| 20 | ) |
| 21 | |
| 22 | // `Layer.launch` converts the layer into a long-running Effect<never>. |
| 23 | export const main = Layer.launch(HttpServerLive) |
| 24 | |
| 25 | // This entrypoint pattern works well when the whole app is represented as |
| 26 | // layers (for example: HTTP server + background workers). |
| 27 | NodeRuntime.runMain(main) |