1

帮助我使用 ZLayers 定义一个 http4s。我正在学习,我很困惑。我想将 http 服务器作为一个组件。但我不知道如何组合 ZManageds 和 ZLayers 以便编译。

创建一个需要Runtime[ZEnv]? ZEnv或者创建一个需要 a 的层并为其生成运行时是否更有意义。

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

1

解决方案是一个ZLayer当持有一个Managed时,本身就是一个Managed。因此,当在这样的层上运行某些东西时,包含的 Managed 将在这段时间内使用。因此,如果您ZIO.never在层上运行 a,则服务器将保持活动状态。

构建层本身是另一个使用技巧fromManaged。而且只createHttp4Server需要一个,我现在承认ZEnv没有任何意义。是他们不使用的另一个技巧。RuntimeZManaged.runtimeZManaged.accessManaged { implicit runtime: Runtime[ZEnv] =>

完整解决方案https://github.com/kovacshuni/zio-http4s-zlayer-example

object Main extends App {

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

    val program: ZIO[Has[Server] with Console, Nothing, Nothing] =
      ZIO.never

    val httpServerLayer: ZLayer[ZEnv, Throwable, Http4Server] = Http4Server.createHttp4sLayer

    program
      .provideLayer(httpServerLayer ++ Console.live)
      .exitCode
  }

}
import zio._
import zio.interop.catz._
import zio.interop.catz.implicits._

import org.http4s.server.Server
import org.http4s.server.blaze.BlazeServerBuilder

object Http4Server {

  type Http4Server = Has[Server]

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

  def createHttp4sLayer: ZLayer[ZEnv, Throwable, Http4Server] =
    ZLayer.fromManaged(createHttp4Server)

}
于 2020-10-07T20:34:44.020 回答