From f9f9e4db34d9ee545833b7be5844d6ce9084a0c3 Mon Sep 17 00:00:00 2001 From: amitsingh Date: Wed, 23 Feb 2022 14:21:02 +0530 Subject: [PATCH] convert `ServerResponseHandler` to class. --- zio-http/src/main/scala/zhttp/http/Http.scala | 6 ++-- .../main/scala/zhttp/service/Handler.scala | 30 +++++++++---------- .../src/main/scala/zhttp/service/Server.scala | 4 ++- .../handlers/ServerResponseHandler.scala | 6 ++-- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/zio-http/src/main/scala/zhttp/http/Http.scala b/zio-http/src/main/scala/zhttp/http/Http.scala index 55ccc8bed6..cf750e3efd 100644 --- a/zio-http/src/main/scala/zhttp/http/Http.scala +++ b/zio-http/src/main/scala/zhttp/http/Http.scala @@ -5,7 +5,7 @@ import io.netty.channel.ChannelHandler import io.netty.handler.codec.http.HttpHeaderNames import zhttp.html._ import zhttp.http.headers.HeaderModifier -import zhttp.service.server.ServerTime +import zhttp.service.server.content.handlers.ServerResponseHandler import zhttp.service.{Handler, HttpRuntime, Server} import zio._ import zio.clock.Clock @@ -453,11 +453,11 @@ object Http { private[zhttp] def compile[R1 <: R]( zExec: HttpRuntime[R1], settings: Server.Config[R1, Throwable], - serverTimeGenerator: ServerTime, + serverResponseHandler: ServerResponseHandler[R1], )(implicit evE: E <:< Throwable, ): ChannelHandler = - Handler(http.asInstanceOf[HttpApp[R1, Throwable]], zExec, settings, serverTimeGenerator) + Handler(http.asInstanceOf[HttpApp[R1, Throwable]], zExec, settings, serverResponseHandler) } /** diff --git a/zio-http/src/main/scala/zhttp/service/Handler.scala b/zio-http/src/main/scala/zhttp/service/Handler.scala index 5667e673cd..a84cdf39bd 100644 --- a/zio-http/src/main/scala/zhttp/service/Handler.scala +++ b/zio-http/src/main/scala/zhttp/service/Handler.scala @@ -4,8 +4,8 @@ import io.netty.channel.ChannelHandler.Sharable import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler} import io.netty.handler.codec.http._ import zhttp.http._ +import zhttp.service.server.WebSocketUpgrade import zhttp.service.server.content.handlers.ServerResponseHandler -import zhttp.service.server.{ServerTime, WebSocketUpgrade} import zio.{UIO, ZIO} import java.net.{InetAddress, InetSocketAddress} @@ -15,11 +15,10 @@ private[zhttp] final case class Handler[R]( app: HttpApp[R, Throwable], runtime: HttpRuntime[R], config: Server.Config[R, Throwable], - serverTimeGenerator: ServerTime, + serverResponseHandler: ServerResponseHandler[R], ) extends SimpleChannelInboundHandler[FullHttpRequest](false) - with WebSocketUpgrade[R] - with ServerResponseHandler[R] { self => - + with WebSocketUpgrade[R] { self => + type Ctx = ChannelHandlerContext override def channelRead0(ctx: Ctx, jReq: FullHttpRequest): Unit = { jReq.touch("server.Handler-channelRead0") implicit val iCtx: ChannelHandlerContext = ctx @@ -60,14 +59,14 @@ private[zhttp] final case class Handler[R]( { case Some(cause) => UIO { - writeResponse( + serverResponseHandler.writeResponse( Response.fromHttpError(HttpError.InternalServerError(cause = Some(cause))), jReq, ) } case None => UIO { - writeResponse(Response.status(Status.NOT_FOUND), jReq) + serverResponseHandler.writeResponse(Response.status(Status.NOT_FOUND), jReq) } }, @@ -76,7 +75,7 @@ private[zhttp] final case class Handler[R]( else { for { _ <- ZIO { - writeResponse(res, jReq) + serverResponseHandler.writeResponse(res, jReq) } } yield () }, @@ -87,14 +86,17 @@ private[zhttp] final case class Handler[R]( if (self.isWebSocket(res)) { self.upgradeToWebSocket(ctx, jReq, res) } else { - writeResponse(res, jReq): Unit + serverResponseHandler.writeResponse(res, jReq): Unit } case HExit.Failure(e) => - writeResponse(Response.fromHttpError(HttpError.InternalServerError(cause = Some(e))), jReq): Unit + serverResponseHandler.writeResponse( + Response.fromHttpError(HttpError.InternalServerError(cause = Some(e))), + jReq, + ): Unit case HExit.Empty => - writeResponse(Response.fromHttpError(HttpError.NotFound(Path(jReq.uri()))), jReq): Unit + serverResponseHandler.writeResponse(Response.fromHttpError(HttpError.NotFound(Path(jReq.uri()))), jReq): Unit } } @@ -103,14 +105,10 @@ private[zhttp] final case class Handler[R]( * Executes program */ private def unsafeRunZIO(program: ZIO[R, Throwable, Any])(implicit ctx: Ctx): Unit = - rt.unsafeRun(ctx) { + runtime.unsafeRun(ctx) { program } - override def serverTime: ServerTime = serverTimeGenerator - - override val rt: HttpRuntime[R] = runtime - override def exceptionCaught(ctx: Ctx, cause: Throwable): Unit = { config.error.fold(super.exceptionCaught(ctx, cause))(f => runtime.unsafeRun(ctx)(f(cause))) } diff --git a/zio-http/src/main/scala/zhttp/service/Server.scala b/zio-http/src/main/scala/zhttp/service/Server.scala index 2753b51f33..dd528e2066 100644 --- a/zio-http/src/main/scala/zhttp/service/Server.scala +++ b/zio-http/src/main/scala/zhttp/service/Server.scala @@ -7,6 +7,7 @@ import zhttp.http.Http._ import zhttp.http.{Http, HttpApp} import zhttp.service.server.ServerSSLHandler._ import zhttp.service.server._ +import zhttp.service.server.content.handlers.ServerResponseHandler import zio.{ZManaged, _} import java.net.{InetAddress, InetSocketAddress} @@ -234,7 +235,8 @@ object Server { channelFactory <- ZManaged.access[ServerChannelFactory](_.get) eventLoopGroup <- ZManaged.access[EventLoopGroup](_.get) zExec <- HttpRuntime.sticky[R](eventLoopGroup).toManaged_ - reqHandler = settings.app.compile(zExec, settings, ServerTime.make) + responseHandler = new ServerResponseHandler[R](zExec, ServerTime.make) + reqHandler = settings.app.compile(zExec, settings, responseHandler) init = ServerChannelInitializer(zExec, settings, reqHandler) serverBootstrap = new ServerBootstrap().channelFactory(channelFactory).group(eventLoopGroup) chf <- ZManaged.effect(serverBootstrap.childHandler(init).bind(settings.address)) diff --git a/zio-http/src/main/scala/zhttp/service/server/content/handlers/ServerResponseHandler.scala b/zio-http/src/main/scala/zhttp/service/server/content/handlers/ServerResponseHandler.scala index 28db13c94a..17153be86a 100644 --- a/zio-http/src/main/scala/zhttp/service/server/content/handlers/ServerResponseHandler.scala +++ b/zio-http/src/main/scala/zhttp/service/server/content/handlers/ServerResponseHandler.scala @@ -13,9 +13,7 @@ import zio.{UIO, ZIO} import java.io.RandomAccessFile @Sharable -private[zhttp] trait ServerResponseHandler[R] { - def serverTime: ServerTime - val rt: HttpRuntime[R] +private[zhttp] final class ServerResponseHandler[R](runtime: HttpRuntime[R], serverTime: ServerTime) { type Ctx = ChannelHandlerContext @@ -24,7 +22,7 @@ private[zhttp] trait ServerResponseHandler[R] { ctx.write(encodeResponse(msg)) msg.data match { case HttpData.BinaryStream(stream) => - rt.unsafeRun(ctx) { + runtime.unsafeRun(ctx) { writeStreamContent(stream).ensuring(UIO(releaseRequest(jReq))) } case HttpData.RandomAccessFile(raf) =>