MCPcopy Create free account
hub / github.com/Effect-TS/effect / serve

Function serve

packages/platform/src/HttpLayerRouter.ts:1078–1128  ·  view source on GitHub ↗
(
  appLayer: Layer.Layer<A, E, R>,
  options?: {
    readonly routerConfig?: Partial<FindMyWay.RouterConfig> | undefined
    readonly disableLogger?: boolean | undefined
    readonly disableListenLog?: boolean
    /**
     * Middleware to apply to the HTTP server.
     *
     * NOTE: This middleware is applied to the entire HTTP server chain,
     * including the sending of the response. This means that modifications
     * to the response **WILL NOT** be reflected in the final response sent to the
     * client.
     *
     * Use HttpLayerRouter.middleware to create middleware that can modify the
     * response.
     */
    readonly middleware?: (
      effect: Effect.Effect<
        HttpServerResponse.HttpServerResponse,
        Request.Only<"Error", R> | Request.Only<"GlobalError", R> | HttpServerError.RouteNotFound,
        | Scope.Scope
        | HttpServerRequest.HttpServerRequest
        | Request.Only<"Requires", R>
        | Request.Only<"GlobalRequires", R>
      >
    ) => Effect.Effect<HttpServerResponse.HttpServerResponse, HE, HR>
  }
)

Source from the content-addressed store, hash-verified

1076 * @category Server
1077 */
1078export const serve = <A, E, R, HE, HR = Request.Only<"Requires", R> | Request.Only<"GlobalRequires", R>>(
1079 appLayer: Layer.Layer<A, E, R>,
1080 options?: {
1081 readonly routerConfig?: Partial<FindMyWay.RouterConfig> | undefined
1082 readonly disableLogger?: boolean | undefined
1083 readonly disableListenLog?: boolean
1084 /**
1085 * Middleware to apply to the HTTP server.
1086 *
1087 * NOTE: This middleware is applied to the entire HTTP server chain,
1088 * including the sending of the response. This means that modifications
1089 * to the response **WILL NOT** be reflected in the final response sent to the
1090 * client.
1091 *
1092 * Use HttpLayerRouter.middleware to create middleware that can modify the
1093 * response.
1094 */
1095 readonly middleware?: (
1096 effect: Effect.Effect<
1097 HttpServerResponse.HttpServerResponse,
1098 Request.Only<"Error", R> | Request.Only<"GlobalError", R> | HttpServerError.RouteNotFound,
1099 | Scope.Scope
1100 | HttpServerRequest.HttpServerRequest
1101 | Request.Only<"Requires", R>
1102 | Request.Only<"GlobalRequires", R>
1103 >
1104 ) => Effect.Effect<HttpServerResponse.HttpServerResponse, HE, HR>
1105 }
1106): Layer.Layer<
1107 A,
1108 Request.Without<E>,
1109 HttpServer.HttpServer | Exclude<Request.Without<R> | Exclude<HR, GlobalProvided>, HttpRouter>
1110> => {
1111 let middleware: any = options?.middleware
1112 if (options?.disableLogger !== true) {
1113 middleware = middleware ? compose(middleware, HttpMiddleware.logger) : HttpMiddleware.logger
1114 }
1115 const RouterLayer = options?.routerConfig
1116 ? Layer.provide(layer, Layer.succeed(RouterConfig, options.routerConfig))
1117 : layer
1118 return Effect.gen(function*() {
1119 const router = yield* HttpRouter
1120 const handler = router.asHttpEffect()
1121 return middleware ? HttpServer.serve(handler, middleware) : HttpServer.serve(handler)
1122 }).pipe(
1123 Layer.unwrapScoped,
1124 Layer.provideMerge(appLayer),
1125 Layer.provide(RouterLayer),
1126 options?.disableListenLog ? identity : HttpServer.withLogAddress
1127 ) as any
1128}
1129
1130/**
1131 * @since 1.0.0

Callers

nothing calls this directly

Calls 3

provideMethod · 0.65
pipeMethod · 0.65
composeFunction · 0.50

Tested by

no test coverage detected