diff --git a/packages/platform-node/examples/http-tag-router.ts b/packages/platform-node/examples/http-tag-router.ts index 99ea0709702..f6b62f778a6 100644 --- a/packages/platform-node/examples/http-tag-router.ts +++ b/packages/platform-node/examples/http-tag-router.ts @@ -1,6 +1,6 @@ import { HttpMiddleware, HttpRouter, HttpServer, HttpServerResponse } from "@effect/platform" import { NodeHttpServer, NodeRuntime } from "@effect/platform-node" -import { Effect, Layer } from "effect" +import { Effect, Layer, PubSub, Stream } from "effect" import { createServer } from "http" // You can define router instances using `HttpRouter.Tag` @@ -9,7 +9,14 @@ class UserRouter extends HttpRouter.Tag("UserRouter")() {} // Create `Layer`'s for your routes with `UserRouter.use` const GetUsers = UserRouter.use((router) => Effect.gen(function*() { + const ps = yield* PubSub.unbounded() + // the following would work, but why doesn't the http server interrupt the stream reading? + // yield* Effect.addFinalizer(() => ps.shutdown) yield* router.get("/", HttpServerResponse.text("got users")) + yield* router.get( + "/stream", + Stream.fromPubSub(ps, { scoped: true }).pipe(Effect.map(HttpServerResponse.stream)) + ) }) ) @@ -41,4 +48,5 @@ const HttpLive = HttpRouter.Default.unwrap(HttpServer.serve(HttpMiddleware.logge Layer.provide(ServerLive) ) +console.log("pid", process.pid) NodeRuntime.runMain(Layer.launch(HttpLive))