| 172 | middleware.add(middleware_ as any) |
| 173 | }), |
| 174 | asHttpEffect() { |
| 175 | let handler = Effect.withFiberRuntime<HttpServerResponse.HttpServerResponse, unknown>((fiber) => { |
| 176 | const contextMap = new Map(fiber.currentContext.unsafeMap) |
| 177 | const request = contextMap.get(HttpServerRequest.HttpServerRequest.key) as HttpServerRequest.HttpServerRequest |
| 178 | let result = router.find(request.method, request.url) |
| 179 | if (result === undefined && request.method === "HEAD") { |
| 180 | result = router.find("GET", request.url) |
| 181 | } |
| 182 | if (result === undefined) { |
| 183 | return Effect.fail(new HttpServerError.RouteNotFound({ request })) |
| 184 | } |
| 185 | const route = result.handler |
| 186 | if (route.prefix._tag === "Some") { |
| 187 | contextMap.set(HttpServerRequest.HttpServerRequest.key, sliceRequestUrl(request, route.prefix.value)) |
| 188 | } |
| 189 | contextMap.set(HttpServerRequest.ParsedSearchParams.key, result.searchParams) |
| 190 | contextMap.set(RouteContext.key, { |
| 191 | [RouteContextTypeId]: RouteContextTypeId, |
| 192 | route, |
| 193 | params: result.params |
| 194 | }) |
| 195 | |
| 196 | const span = contextMap.get(Tracer.ParentSpan.key) as Tracer.Span | undefined |
| 197 | if (span && span._tag === "Span") { |
| 198 | span.attribute("http.route", route.path) |
| 199 | } |
| 200 | return Effect.locally( |
| 201 | (route.uninterruptible ? |
| 202 | route.handler : |
| 203 | Effect.interruptible(route.handler)) as Effect.Effect< |
| 204 | HttpServerResponse.HttpServerResponse, |
| 205 | unknown |
| 206 | >, |
| 207 | FiberRef.currentContext, |
| 208 | Context.unsafeMake(contextMap) |
| 209 | ) |
| 210 | }) |
| 211 | if (middleware.size === 0) return handler |
| 212 | for (const fn of Arr.reverse(middleware)) { |
| 213 | handler = fn(handler as any) |
| 214 | } |
| 215 | return handler |
| 216 | } |
| 217 | }) |
| 218 | }) |
| 219 | |