tapir icon indicating copy to clipboard operation
tapir copied to clipboard

[doc][help] Creating and composing routes with ZIO

Open ex0ns opened this issue 3 years ago • 0 comments

As discussed on Gitter, I was playing with tapir and ZIO. My main goal was to move from

ZLayer.fromService[GameService, ZRoutes] { gameService =>
      val r = ZHttp4sServerInterpreter
        .from(
          List(
            endpoints.startGame.zServerLogic(_ => gameService.startGame()),
          )
        )
        .toRoutes
      r <+> swaggerRoutes
    }

To a more (IMO) 'zio' definition by using GameService.startGame() instead of using the gameService created using the fromService method. I ended up with something like that:

def routes2 = {
   val r: HttpRoutes[RoutesTest] = ZHttp4sServerInterpreter
     .from[Has[GameService]](
       List(
         endpoints.startGame.zServerLogic(_ => GameService.startGame())
       )
     )
     .toRoutes
   r <+>
     new SwaggerHttp4s(swaggerDefinition, contextPath = "docs").routes[RoutesTest]
 }

Unfortunately, this changed the type return of my method from a ZLayer that I could compose with the rest of my application to a HttpRoutes (based on cats kleisli if I understood correctly).

For reference, I would like to start my server with the following code:

  val routes = Router.routes
  val service = Service.live

  val routesLayer = service >>> routes

  (zio.console.putStrLn("Server starting") *> server).provideSomeLayer[ZEnv](routesLayer).exitCode

I've written a sample application on Scastie: https://scastie.scala-lang.org/gAaqGmJdQTyIT9gRx4REHQ that show the issue I have.

I think that in the end the main question is: how to convert from Cats' Kleisli to ZIO's ZLayer ?

ex0ns avatar Apr 12 '21 16:04 ex0ns