Combining Two Routers with RPC and HTTP Routes in Typescript
I'm struggling to put these two routers together (one with my RPC handler and one with my HTTP routes). I've inlayed the type annotations for context.
// RPC things
const RPCLayer: Layer.Layer<
never,
never,
HttpServer.HttpServer | AppDB | ErrorReporter | CurrentUser | RequestContext | WorkOSClient
> = HttpRouter.Default.serve(HttpMiddleware.logger).pipe(
Layer.provide(RPCLive),
Layer.provide(RPCHttpProtocol),
// TODO - (I think) Move existing router into this layer
);
// Create the main router with all routes
const existingRouter: Effect.Effect<
HttpServerResponse.HttpServerResponse,
RouteNotFound | ConfigError.ConfigError | InternalError,
HttpServerRequest.HttpServerRequest | AppDB | Scope.Scope | DeployInfoContext
> = HttpRouter.empty.pipe(
HttpRouter.use(httpAuthMiddleware),
HttpRouter.get(
"/",
E.gen(function* (_) {
const req = yield* _(RequestContext);
return yield* HttpServerResponse.text(
`@phosphor/server (${req.release} ${req.environment} ${req.server_notes})`,
);
}),
),
HttpRouter.mount("/test-errors", testErrorsRouter),
HttpRouter.mount("/test-health", testHealthRouter),
HttpRouter.mount("/auth/workos", workOsRouter), // RPC things
const RPCLayer: Layer.Layer<
never,
never,
HttpServer.HttpServer | AppDB | ErrorReporter | CurrentUser | RequestContext | WorkOSClient
> = HttpRouter.Default.serve(HttpMiddleware.logger).pipe(
Layer.provide(RPCLive),
Layer.provide(RPCHttpProtocol),
// TODO - (I think) Move existing router into this layer
);
// Create the main router with all routes
const existingRouter: Effect.Effect<
HttpServerResponse.HttpServerResponse,
RouteNotFound | ConfigError.ConfigError | InternalError,
HttpServerRequest.HttpServerRequest | AppDB | Scope.Scope | DeployInfoContext
> = HttpRouter.empty.pipe(
HttpRouter.use(httpAuthMiddleware),
HttpRouter.get(
"/",
E.gen(function* (_) {
const req = yield* _(RequestContext);
return yield* HttpServerResponse.text(
`@phosphor/server (${req.release} ${req.environment} ${req.server_notes})`,
);
}),
),
HttpRouter.mount("/test-errors", testErrorsRouter),
HttpRouter.mount("/test-health", testHealthRouter),
HttpRouter.mount("/auth/workos", workOsRouter),