Skip to content

Commit

Permalink
chore: Upgrade zio-http to version 3.0.0-RC7
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubjanecek committed May 24, 2024
1 parent edeecc9 commit 949971e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 47 deletions.
4 changes: 2 additions & 2 deletions doc/server/ziohttp.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ example:
import sttp.tapir.PublicEndpoint
import sttp.tapir.ztapir._
import sttp.tapir.server.ziohttp.ZioHttpInterpreter
import zio.http.{HttpApp, Request, Response}
import zio.http.{Request, Response, Routes}
import zio._

def countCharacters(s: String): ZIO[Any, Nothing, Int] =
Expand All @@ -54,7 +54,7 @@ def countCharacters(s: String): ZIO[Any, Nothing, Int] =
val countCharactersEndpoint: PublicEndpoint[String, Unit, Int, Any] =
endpoint.in(stringBody).out(plainBody[Int])

val countCharactersHttp: HttpApp[Any] =
val countCharactersHttp: Routes[Any, Response] =
ZioHttpInterpreter().toHttp(countCharactersEndpoint.zServerLogic(countCharacters))
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend
import sttp.model.HeaderNames
import sttp.tapir.server.ziohttp.ZioHttpInterpreter
import sttp.tapir.ztapir._
import zio.http.{HttpApp, Server}
import zio.http.{Response => ZioHttpResponse, Routes, Server}
import zio.{Console, ExitCode, IO, Scope, Task, ZIO, ZIOAppDefault, ZLayer}

object ServerSecurityLogicZio extends ZIOAppDefault {
Expand Down Expand Up @@ -55,7 +55,7 @@ object ServerSecurityLogicZio extends ZIOAppDefault {
// ---

// interpreting as an app
val routes: HttpApp[Any] = ZioHttpInterpreter().toHttp(secureHelloWorldWithLogic)
val routes: Routes[Any, ZioHttpResponse] = ZioHttpInterpreter().toHttp(secureHelloWorldWithLogic)

override def run: ZIO[Scope, Throwable, ExitCode] = {
def testWith(backend: SttpBackend[Task, Any], port: Int, path: String, salutation: String, token: String): Task[String] =
Expand Down
2 changes: 1 addition & 1 deletion project/Versions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object Versions {
val iron = "2.5.0"
val enumeratum = "1.7.3"
val zio = "2.1.1"
val zioHttp = "3.0.0-RC6"
val zioHttp = "3.0.0-RC7"
val zioInteropCats = "23.0.0.8"
val zioInteropReactiveStreams = "2.0.2"
val zioJson = "0.6.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import zio.http.{Header => ZioHttpHeader, Headers => ZioHttpHeaders, _}
trait ZioHttpInterpreter[R] {
def zioHttpServerOptions: ZioHttpServerOptions[R] = ZioHttpServerOptions.default

def toHttp[R2](se: ZServerEndpoint[R2, ZioStreams with WebSockets]): HttpApp[R & R2] =
def toHttp[R2](se: ZServerEndpoint[R2, ZioStreams with WebSockets]): Routes[R & R2, Response] =
toHttp(List(se))

def toHttp[R2](ses: List[ZServerEndpoint[R2, ZioStreams with WebSockets]]): HttpApp[R & R2] = {
def toHttp[R2](ses: List[ZServerEndpoint[R2, ZioStreams with WebSockets]]): Routes[R & R2, Response] = {
implicit val bodyListener: ZioHttpBodyListener[R & R2] = new ZioHttpBodyListener[R & R2]
implicit val monadError: MonadError[RIO[R & R2, *]] = new RIOMonadError[R & R2]
val widenedSes = ses.map(_.widen[R & R2])
Expand All @@ -27,42 +27,40 @@ trait ZioHttpInterpreter[R] {
val zioHttpResponseBody = new ZioHttpToResponseBody
val interceptors = RejectInterceptor.disableWhenSingleEndpoint(widenedServerOptions.interceptors, widenedSes)

HttpApp(
Routes.singleton {
Handler.fromFunctionZIO[(Path, Request)] { case (_: Path, request: Request) =>
val interpreter = new ServerInterpreter[ZioStreams with WebSockets, RIO[R & R2, *], ZioResponseBody, ZioStreams](
FilterServerEndpoints(widenedSes),
zioHttpRequestBody,
zioHttpResponseBody,
interceptors,
zioHttpServerOptions.deleteFile
)
Routes.singleton {
Handler.fromFunctionZIO[(Path, Request)] { case (_: Path, request: Request) =>
val interpreter = new ServerInterpreter[ZioStreams with WebSockets, RIO[R & R2, *], ZioResponseBody, ZioStreams](
FilterServerEndpoints(widenedSes),
zioHttpRequestBody,
zioHttpResponseBody,
interceptors,
zioHttpServerOptions.deleteFile
)

if (request.url.encode.trim.isEmpty) {
ZIO.logError("Received an apparently empty request URI, not handling: " + request) *>
ZIO.fail(Response.internalServerError("Empty request URI"))
} else {
val serverRequest = ZioHttpServerRequest(request)
interpreter
.apply(serverRequest)
.foldCauseZIO(
cause => ZIO.logErrorCause(cause) *> ZIO.fail(Response.internalServerError(cause.squash.getMessage)),
{
case RequestResult.Response(resp) =>
resp.body match {
case None => handleHttpResponse(resp, None)
case Some(Right(body)) => handleHttpResponse(resp, Some(body))
case Some(Left(body)) => handleWebSocketResponse(body, zioHttpServerOptions.customWebSocketConfig(serverRequest))
}
if (request.url.encode.trim.isEmpty) {
ZIO.logError("Received an apparently empty request URI, not handling: " + request) *>
ZIO.fail(Response.internalServerError("Empty request URI"))
} else {
val serverRequest = ZioHttpServerRequest(request)
interpreter
.apply(serverRequest)
.foldCauseZIO(
cause => ZIO.logErrorCause(cause) *> ZIO.fail(Response.internalServerError(cause.squash.getMessage)),
{
case RequestResult.Response(resp) =>
resp.body match {
case None => handleHttpResponse(resp, None)
case Some(Right(body)) => handleHttpResponse(resp, Some(body))
case Some(Left(body)) => handleWebSocketResponse(body, zioHttpServerOptions.customWebSocketConfig(serverRequest))
}

case RequestResult.Failure(_) =>
ZIO.fail(Response.notFound)
}
)
}
case RequestResult.Failure(_) =>
ZIO.fail(Response.notFound)
}
)
}
}
)
}
}

private def handleWebSocketResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import sttp.client3._
import sttp.model.StatusCode
import sttp.tapir.server.tests.CreateServerTest
import sttp.tapir.ztapir._
import zio.http.{endpoint => _, _}
import zio.http.{Response => ZioHttpResponse, endpoint => _, _}
import zio.{Task, ZIO}

class ZioHttpCompositionTest(
createServerTest: CreateServerTest[
Task,
Any,
ZioHttpServerOptions[Any],
HttpApp[Any]
Routes[Any, ZioHttpResponse]
]
) {
import createServerTest._
Expand All @@ -26,9 +26,9 @@ class ZioHttpCompositionTest(
val ep1 = endpoint.get.in("p1").zServerLogic[Any](_ => ZIO.unit)
val ep3 = endpoint.get.in("p3").zServerLogic[Any](_ => ZIO.fail(new RuntimeException("boom")))

val route1: HttpApp[Any] = ZioHttpInterpreter().toHttp(ep1)
val route2: HttpApp[Any] = Routes(Method.GET / "p2" -> handler(zio.http.Response.ok)).toHttpApp
val route3: HttpApp[Any] = ZioHttpInterpreter().toHttp(ep3)
val route1: Routes[Any, ZioHttpResponse] = ZioHttpInterpreter().toHttp(ep1)
val route2: Routes[Any, ZioHttpResponse] = Routes(Method.GET / "p2" -> handler(ZioHttpResponse.ok))
val route3: Routes[Any, ZioHttpResponse] = ZioHttpInterpreter().toHttp(ep3)

NonEmptyList.of(route3, route1, route2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ class ZioHttpTestServerInterpreter(
channelFactory: ZLayer[Any, Nothing, ChannelFactory[ServerChannel]]
)(implicit
trace: Trace
) extends TestServerInterpreter[Task, ZioStreams with WebSockets, ZioHttpServerOptions[Any], HttpApp[Any]] {
) extends TestServerInterpreter[Task, ZioStreams with WebSockets, ZioHttpServerOptions[Any], Routes[Any, Response]] {

override def route(
es: List[ServerEndpoint[ZioStreams with WebSockets, Task]],
interceptors: Interceptors
): HttpApp[Any] = {
): Routes[Any, Response] = {
val serverOptions: ZioHttpServerOptions[Any] = interceptors(ZioHttpServerOptions.customiseInterceptors).options
ZioHttpInterpreter(serverOptions).toHttp(es)
}

override def serverWithStop(
routes: NonEmptyList[HttpApp[Any]],
routes: NonEmptyList[Routes[Any, Response]],
gracefulShutdownTimeout: Option[FiniteDuration]
): Resource[IO, (Port, KillSwitch)] = {
implicit val r: Runtime[Any] = Runtime.default
Expand Down

0 comments on commit 949971e

Please sign in to comment.