for {
_ <- IO
.fromFuture(IO(Http().bindAndHandle(routes, settings.httpInterface, settings.httpPort)))
.bracket { serverBinding =>
actorSystem.log.info("API listens on {}", serverBinding.localAddress)
IO.never
}(serverBinding => IO(serverBinding.terminate(1.minute)))
} yield ()
@chetkhatri for akka http servers I do this:
def httpServer(routes: Routes): Resource[IO, ServerBinding] = {
val serverF = IO.deferFuture {
IO{
Http().bindAndHandle(
routes,
config.http.host,
config.http.port
)
}
}
Resource.make(serverF)(server => IO.fromFuture(IO(server.unbind())).void)
}
Then use it like this:
def run(args: List[String]): IO[ExitCode] = {
val appResource: Resource[F, Unit] = for {
service1 <- ...
service2 <- ...
_ <- httpServer(service1.routes ~ service2.routes)
} yield ()
appResource.use(_ => IO.never)
}
Resource[F, Unit]
, which is actually fine :)
So it is useful for suspending effects
suspending effects
mean?
val hi: IO[Unit] = IO(println("hi"))
<- prints nothing when constructing hi
Asynchronous boundaries
mean in context with https://typelevel.org/cats-effect/datatypes/io.html#concurrency-and-cancellation