Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert ServerResponseHandler to class. #1086

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions zio-http/src/main/scala/zhttp/http/Http.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

/**
Expand Down
30 changes: 14 additions & 16 deletions zio-http/src/main/scala/zhttp/service/Handler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type Ctx = ChannelHandlerContext
type Ctx = ChannelHandlerContext
val responseService = new ServerResponseHandler[R](zExec, ServerTime.make)

override def channelRead0(ctx: Ctx, jReq: FullHttpRequest): Unit = {
jReq.touch("server.Handler-channelRead0")
implicit val iCtx: ChannelHandlerContext = ctx
Expand Down Expand Up @@ -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)
}

},
Expand All @@ -76,7 +75,7 @@ private[zhttp] final case class Handler[R](
else {
for {
_ <- ZIO {
writeResponse(res, jReq)
serverResponseHandler.writeResponse(res, jReq)
}
} yield ()
},
Expand All @@ -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

}
}
Expand All @@ -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)))
}
Expand Down
4 changes: 3 additions & 1 deletion zio-http/src/main/scala/zhttp/service/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import zio.{UIO, ZIO}
import java.io.RandomAccessFile

@Sharable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Sharable

private[zhttp] trait ServerResponseHandler[R] {
def serverTime: ServerTime
val rt: HttpRuntime[R]
private[zhttp] final class ServerResponseHandler[R](runtime: HttpRuntime[R], serverTime: ServerTime) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private[zhttp] final class ServerResponseHandler[R](runtime: HttpRuntime[R], serverTime: ServerTime) {
private[zhttp] final class HttpResponseService[R](runtime: HttpRuntime[R], serverTime: ServerTime) {


type Ctx = ChannelHandlerContext

Expand All @@ -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) =>
Expand Down