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

Upgrade ZIO HTTP (0.0.5) + Cats Effect Interop (23.0.0.2) #2794

Merged
merged 1 commit into from
Mar 20, 2023
Merged
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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ lazy val zioHttpServer: ProjectMatrix = (projectMatrix in file("server/zio-http-
.settings(commonJvmSettings)
.settings(
name := "tapir-zio-http-server",
libraryDependencies ++= Seq("dev.zio" %% "zio-interop-cats" % Versions.zioInteropCats % Test, "dev.zio" %% "zio-http" % "0.0.4")
libraryDependencies ++= Seq("dev.zio" %% "zio-interop-cats" % Versions.zioInteropCats % Test, "dev.zio" %% "zio-http" % "0.0.5")
)
.jvmPlatform(scalaVersions = scala2And3Versions)
.dependsOn(serverCore, zio, serverTests % Test)
Expand Down
2 changes: 1 addition & 1 deletion project/Versions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object Versions {
val zio1Json = "0.2.0"
val zio1InteropReactiveStreams = "1.3.12"
val zio = "2.0.10"
val zioInteropCats = "3.3.0"
val zioInteropCats = "23.0.0.2"
val zioInteropReactiveStreams = "2.0.1"
val zioJson = "0.4.2"
val playClient = "2.1.10"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package sttp.tapir.server.ziohttp

import io.netty.handler.codec.http.HttpResponseStatus
import sttp.capabilities.zio.ZioStreams
import sttp.model.{HeaderNames, Method, Header => SttpHeader}
import sttp.monad.MonadError
Expand Down Expand Up @@ -49,10 +48,11 @@ trait ZioHttpInterpreter[R] {
ZioHttpHeader(HeaderNames.ContentLength, contentLength.toString) :: baseHeaders
case _ => baseHeaders
}
val statusCode = resp.code.code

ZIO.succeed(
Response(
status = Status.fromHttpResponseStatus(HttpResponseStatus.valueOf(resp.code.code)),
status = Status.fromInt(statusCode).getOrElse(Status.Custom(statusCode)),
headers = ZioHttpHeaders(allHeaders),
body = resp.body.map { case (stream, _) => Body.fromStream(stream) }.getOrElse(Body.empty)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ case class ZioHttpServerRequest(req: Request, attributes: AttributeMap = Attribu
case Segment.Root => Nil
}.toList
override lazy val queryParameters: QueryParams = QueryParams.fromMultiMap(req.url.queryParams.toMap)
override lazy val method: SttpMethod = SttpMethod(req.method.toJava.name().toUpperCase)
override lazy val method: SttpMethod = SttpMethod(req.method.name.toUpperCase)
override lazy val uri: Uri = Uri.unsafeParse(req.url.encode)
override lazy val headers: Seq[SttpHeader] = req.headers.toList.map { h => SttpHeader(h.key.toString, h.value.toString) }
override def attribute[T](k: AttributeKey[T]): Option[T] = attributes.get(k)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import sttp.client3._
import sttp.model.StatusCode
import sttp.tapir.server.tests.CreateServerTest
import sttp.tapir.ztapir._
import zio.http._
import zio.http.{endpoint => _, _}
import zio.http.model._
import zio.{Task, ZIO}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sttp.tapir.server.ziohttp

import cats.effect.{IO, Resource}
import io.netty.channel.{ChannelFactory, EventLoopGroup, ServerChannel}
import org.scalatest.Assertion
import org.scalatest.matchers.should.Matchers._
import sttp.capabilities.zio.ZioStreams
Expand All @@ -9,11 +10,11 @@ import sttp.tapir._
import sttp.tapir.server.tests._
import sttp.tapir.tests.{Test, TestSuite}
import sttp.tapir.ztapir.{RIOMonadError, RichZEndpoint}
import zio.http.{Path, Request, URL}
import zio.http.netty.{ChannelType, EventLoopGroups, ChannelFactories}
import zio.http.{HttpAppMiddleware, Path, Request, URL}
import zio.http.netty.{ChannelFactories, ChannelType, EventLoopGroups}
import zio.interop.catz._
import zio.{Runtime, Promise, Ref, Task, UIO, Unsafe, ZIO, ZEnvironment, ZLayer}
import zio.http.Middleware
import zio.{Promise, Ref, Runtime, Task, UIO, Unsafe, ZEnvironment, ZIO, ZLayer}

import java.time

class ZioHttpServerTest extends TestSuite {
Expand All @@ -22,7 +23,7 @@ class ZioHttpServerTest extends TestSuite {
implicit val r: Runtime[Any] = Runtime.default
// creating the netty dependencies once, to speed up tests
Resource
.scoped[IO, Any, ZEnvironment[zio.http.service.EventLoopGroup with zio.http.service.ServerChannelFactory]]({
.scoped[IO, Any, ZEnvironment[EventLoopGroup with ChannelFactory[ServerChannel]]]({
val eventConfig = ZLayer.succeed(new EventLoopGroups.Config {
def channelType = ChannelType.AUTO
val nThreads = 0
Expand All @@ -32,8 +33,8 @@ class ZioHttpServerTest extends TestSuite {
(channelConfig >>> ChannelFactories.Server.fromConfig) ++ (eventConfig >>> EventLoopGroups.fromConfig)
}.build)
.map { nettyDeps =>
val eventLoopGroup = ZLayer.succeed(nettyDeps.get[zio.http.service.EventLoopGroup])
val channelFactory = ZLayer.succeed(nettyDeps.get[zio.http.service.ServerChannelFactory])
val eventLoopGroup = ZLayer.succeed(nettyDeps.get[EventLoopGroup])
val channelFactory = ZLayer.succeed(nettyDeps.get[ChannelFactory[ServerChannel]])
val interpreter = new ZioHttpTestServerInterpreter(eventLoopGroup, channelFactory)
val createServerTest = new DefaultCreateServerTest(backend, interpreter)

Expand All @@ -57,7 +58,7 @@ class ZioHttpServerTest extends TestSuite {
.out(stringBody)
.zServerLogic[Any](_ => p.await.timeout(time.Duration.ofSeconds(1)) *> ZIO.succeed("Ok"))
int = ZioHttpInterpreter().toHttp(ep)
route = int @@ Middleware.allowZIO[Any, Nothing]((_: Request) => p.succeed(()).as(true))
route = int @@ HttpAppMiddleware.allowZIO[Any, Nothing]((_: Request) => p.succeed(()).as(true))
result <- route
.runZIO(Request.get(url = URL(Path.empty / "p1")))
.flatMap(response => response.body.asString)
Expand All @@ -74,7 +75,8 @@ class ZioHttpServerTest extends TestSuite {
.in("p1")
.out(stringBody)
.zServerLogic[Any](_ => ref.updateAndGet(_ + 1).map(_.toString))
route = ZioHttpInterpreter().toHttp(ep) @@ Middleware.allowZIO[Any, Nothing]((_: Request) => ref.update(_ + 1).as(true))
route = ZioHttpInterpreter()
.toHttp(ep) @@ HttpAppMiddleware.allowZIO[Any, Nothing]((_: Request) => ref.update(_ + 1).as(true))
result <- route
.runZIO(Request.get(url = URL(Path.empty / "p1")))
.flatMap(response => response.body.asString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import zio.http._
import zio.interop.catz._

class ZioHttpTestServerInterpreter(
eventLoopGroup: ZLayer[Any, Nothing, zio.http.service.EventLoopGroup],
channelFactory: ZLayer[Any, Nothing, zio.http.service.ServerChannelFactory]
eventLoopGroup: ZLayer[Any, Nothing, EventLoopGroup],
channelFactory: ZLayer[Any, Nothing, ChannelFactory[ServerChannel]]
)(implicit
trace: Trace
) extends TestServerInterpreter[Task, ZioStreams, ZioHttpServerOptions[Any], Http[Any, Throwable, Request, Response]] {
Expand Down
3 changes: 2 additions & 1 deletion server/zio-http-server/src/test/scala/zio/test/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package zio

import io.netty.channel._
import zio.http._
import zio.http.netty.NettyServerConfig
import zio.http.netty.server.NettyDriver

package object test {
// NettyDriver is private[zio] so we need to fake it (will be solved in future zio-http versions)
val driver: ZLayer[EventLoopGroup & ChannelFactory[ServerChannel] & ServerConfig, Nothing, Driver] =
NettyDriver.manual
ZLayer.makeSome[EventLoopGroup & ChannelFactory[ServerChannel] & ServerConfig, Driver](NettyServerConfig.live, NettyDriver.manual)
}