Effect CommunityEC
Effect Community17mo ago
2 replies
Simon

Applying Middlewares to Each Level in Platform-Bun's HttpRouter Example

using the http-tag-router example for platform-bun, how are you supposed to apply middlewares via HttpRouter.use(myMiddleware) to each 'level'? and also other functions like Effect.catchAllDefect

class UserRouter extends HttpRouter.Tag("UserRouter")<UserRouter>() {}

// Create `Layer`'s for your routes with `UserRouter.use`
const GetUsers = UserRouter.use((router) =>
  Effect.gen(function*() {
    yield* router.get("/", HttpServerResponse.text("got users"))
  })
)

...

const AllRoutes = HttpRouter.Default.use((router) =>
  Effect.gen(function*() {
    yield* router.mount("/users", yield* UserRouter.router)
  })
).pipe(Layer.provide(AllUserRoutes))

...

const HttpLive = HttpRouter.Default.unwrap(HttpServer.serve(HttpMiddleware.logger)).pipe(
  HttpServer.withLogAddress,
  Layer.provide(AllRoutes),
  Layer.provide(ServerLive)
)

BunRuntime.runMain(Layer.launch(HttpLive));


I have tried all kinds of combinations but its the .Default that makes it confusing for me since i turns it into a layer instead of HttpRouter - all the examples show the middlewares and other functions being piped when using HttpRouter.empty
Was this page helpful?