MCPcopy
hub / github.com/Effect-TS/effect / serve

Function serve

packages/platform/src/HttpLayerRouter.ts:1058–1108  ·  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

1056 * @category Server
1057 */
1058export const serve = <A, E, R, HE, HR = Request.Only<"Requires", R> | Request.Only<"GlobalRequires", R>>(
1059 appLayer: Layer.Layer<A, E, R>,
1060 options?: {
1061 readonly routerConfig?: Partial<FindMyWay.RouterConfig> | undefined
1062 readonly disableLogger?: boolean | undefined
1063 readonly disableListenLog?: boolean
1064 /**
1065 * Middleware to apply to the HTTP server.
1066 *
1067 * NOTE: This middleware is applied to the entire HTTP server chain,
1068 * including the sending of the response. This means that modifications
1069 * to the response **WILL NOT** be reflected in the final response sent to the
1070 * client.
1071 *
1072 * Use HttpLayerRouter.middleware to create middleware that can modify the
1073 * response.
1074 */
1075 readonly middleware?: (
1076 effect: Effect.Effect<
1077 HttpServerResponse.HttpServerResponse,
1078 Request.Only<"Error", R> | Request.Only<"GlobalError", R> | HttpServerError.RouteNotFound,
1079 | Scope.Scope
1080 | HttpServerRequest.HttpServerRequest
1081 | Request.Only<"Requires", R>
1082 | Request.Only<"GlobalRequires", R>
1083 >
1084 ) => Effect.Effect<HttpServerResponse.HttpServerResponse, HE, HR>
1085 }
1086): Layer.Layer<
1087 A,
1088 Request.Without<E>,
1089 HttpServer.HttpServer | Exclude<Request.Without<R> | Exclude<HR, GlobalProvided>, HttpRouter>
1090> => {
1091 let middleware: any = options?.middleware
1092 if (options?.disableLogger !== true) {
1093 middleware = middleware ? compose(middleware, HttpMiddleware.logger) : HttpMiddleware.logger
1094 }
1095 const RouterLayer = options?.routerConfig
1096 ? Layer.provide(layer, Layer.succeed(RouterConfig, options.routerConfig))
1097 : layer
1098 return Effect.gen(function*() {
1099 const router = yield* HttpRouter
1100 const handler = router.asHttpEffect()
1101 return middleware ? HttpServer.serve(handler, middleware) : HttpServer.serve(handler)
1102 }).pipe(
1103 Layer.unwrapScoped,
1104 Layer.provideMerge(appLayer),
1105 Layer.provide(RouterLayer),
1106 options?.disableListenLog ? identity : HttpServer.withLogAddress
1107 ) as any
1108}
1109
1110/**
1111 * @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

Used in the wild real call sites across dependent graphs

searching dependent graphs…