Skip to content

Instantly share code, notes, and snippets.

@soujiro32167
Created October 30, 2021 19:41
Show Gist options
  • Save soujiro32167/30bea89bc5c8a483929e610c607a4b7e to your computer and use it in GitHub Desktop.
Save soujiro32167/30bea89bc5c8a483929e610c607a4b7e to your computer and use it in GitHub Desktop.
Tapir ZIO Http composition bug
import sttp.client3.asynchttpclient.zio.{AsyncHttpClientZioBackend, SttpClient}
import sttp.tapir.server.ziohttp.ZioHttpInterpreter
import zhttp.http.RHttpApp
import zhttp.service.{EventLoopGroup, Server}
import zhttp.service.server.ServerChannelFactory
import zio.{Runtime, ZIO}
object tapir {
import sttp.tapir.ztapir._
val ep1 = endpoint.get.in("p1").zServerLogic(_ => ZIO.unit)
val ep2 = endpoint.get.in("p2").zServerLogic(_ => ZIO.unit)
val route1 = ZioHttpInterpreter().toHttp(ep1)
val route2 = ZioHttpInterpreter().toHttp(ep2)
val routeCombined = ZioHttpInterpreter().toHttp(List(ep1, ep2))
}
object zioHttp {
import zhttp.http._
val route1 = Http.collect[Request] { case Method.GET -> Root / "p1" =>
Response.ok
}
val route2 = Http.collect[Request] { case Method.GET -> Root / "p2" =>
Response.ok
}
}
def serverClient(app: RHttpApp[Any]) = Server.make(Server.port(8080) ++ Server.app(app)) *>
AsyncHttpClientZioBackend.managed() provideLayer (EventLoopGroup.auto(0) ++ ServerChannelFactory.auto)
def test(backend: SttpClient.Service) = {
import sttp.client3._
basicRequest.get(uri"http://localhost:8080/p1").send(backend) *>
basicRequest.get(uri"http://localhost:8080/p2").send(backend)
}
val tapirComposedRoutes =
serverClient(tapir.routeCombined).use(test)
val tapirZhttpComposedRoutes =
serverClient(tapir.route1 +++ tapir.route2).use(test)
val zhttpComposedRoutes =
serverClient(zioHttp.route1 +++ zioHttp.route2).use(test)
Runtime.default.unsafeRun(tapirZhttpComposedRoutes) // "/p2" not found
// Runtime.default.unsafeRun(tapirComposedRoutes) // OK
// Runtime.default.unsafeRun(zhttpComposedRoutes) // OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment