Effect CommunityEC
Effect Community•12mo ago•
16 replies
canastro

It looks like you're trying to set up an HTTP server with multiple routers using the Effect Types...

I'm trying to provide a few deps to my HttpRouter.Tag. I started with a ManagedRuntime, but I'm having an hard time figuring out how to make it available in a router such as:

This is how I setup my server:

const Routes = HttpRouter.Default.use((router) =>
  Effect.gen(function* () {
    yield* router.mount('/quicknode', yield* QuicknodeRouter.router);
    yield* router.mount('/', yield* SystemRouter.router);
  }),
).pipe(Layer.provide(QuicknodeRoutes), Layer.provide(SystemRoutes));

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

appRuntime.runFork(Layer.launch(HttpLive));


And this an example of one of these subrouters (added the AppLayerRequirements as the second argument in the tag generic):

export class QuicknodeRouter extends HttpRouter.Tag('QuicknodeRouter')<QuicknodeRouter, AppLayerRequirements>() { }

const WebhookRoute = QuicknodeRouter.use((router) =>
  router.post(
    '/webhook',
    Effect.flatMap(HttpServerRequest.HttpServerRequest, () =>
      pipe(
        HttpServerRequest.schemaBodyJson(WebhookRequestPayload),
        Effect.flatMap((json) => handle(json)),
        // Effect.flatMap(() => HttpServerResponse.empty({ status: 201 })),
      ),
    ),
  ),
);
`

But as you can see in the printscreen I'm having troubles passing the requirements into the router.

I'm more used to the pipe versions of Effect, and I'm having troubles figuring out how to convert the router.mount piece into that format in order for me to better understand how this works 😅
image.png
Was this page helpful?