1

ZLayers を使用して http4s を定義するのを手伝ってください。私は学んでいて、混乱しています。http サーバーをコンポーネントとして除外したいと思います。しかし、コンパイルできるように ZManaged と ZLayers を構成する方法がわかりません。

また、必要なレイヤーを作成することは理にかなっていますRuntime[ZEnv]か? それとも、 を必要とするレイヤーを作成し、ZEnvそのランタイムを生成する方が理にかなっているでしょうか。

object HttpServer {

  def createHttp4Server: ZManaged[Runtime[ZEnv], Throwable, Server] = 
      ZManaged.accessManaged { implicit runtime: Runtime[ZEnv] =>
        BlazeServerBuilder[Task](runtime.platform.executor.asEC)
          .bindHttp(8080, "localhost")
          .withHttpApp(Routes.helloWorldService)
          .resource
          .toManagedZIO
      }


  def createHttp4Layer: ZLayer[RuntimeLayer, Throwable, Http4ServerLayer] =
    ZLayer.succeed(createHttp4Server)

}

object Routes {
  val helloWorldService = ...
}
package object simple {
  type Http4ServerLayer = Has[ZManaged[Runtime[ZEnv], Throwable, Server]]
  type RuntimeLayer = Has[Runtime[ZEnv]]
}

メインのここZManaged[..., ..., Server]からへのアクセス方法がわかりません。メソッドが完全にLayer理解できていません。access

object ItsAren extends App {

  def run(args: List[String]): URIO[ZEnv, ExitCode] = {

    val toProvide: ZIO[Http4ServerLayer, Nothing, ExitCode] =
      ZIO
        .accessM[Http4ServerLayer](_.get.useForever) //compile error
        .as(ExitCode.success)
        .catchAll(ZIO.succeed(ExitCode.failure))

    val runtimeLayer: ZLayer[Any, Nothing, RuntimeLayer]              = ZLayer.succeed(Runtime.default)
    val http4Layer: ZLayer[RuntimeLayer, Throwable, Http4ServerLayer] = HttpServer.createHttp4Layer
    val fullLayer: ZLayer[Any, Throwable, Http4ServerLayer]           = runtimeLayer >>> http4Layer

    val provided = toProvide.provideCustomLayer(fullLayer)

    provided //compile error
  }

}
type mismatch
 found   : ZIO[Runtime[ZEnv], Throwable, Nothing]
 required: ZIO[Http4ServerLayer, ?, ?]

一番下にもありますが、それは重要性が低いです

 found   : ZIO[ZEnv, Throwable, ExitCode]
 required: URIO[ZEnv, ExitCode]

PR で同じことをコメントして ください https://github.com/kovacshuni/itsaren/pull/1

4

1 に答える 1