Skip to content

Instantly share code, notes, and snippets.

@serragnoli
Created October 28, 2019 17:51
Show Gist options
  • Select an option

  • Save serragnoli/0305e935c5e5a977b380cbd862a4872a to your computer and use it in GitHub Desktop.

Select an option

Save serragnoli/0305e935c5e5a977b380cbd862a4872a to your computer and use it in GitHub Desktop.
Akka-http with ZIO
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import zio._
import zio.console._
import scala.concurrent.ExecutionContextExecutor
import scala.io.StdIn
trait Helpers {
implicit class ZioExtension[R, E, A](z: ZIO[R, E, A]) {
def exited: ZIO[R, Nothing, Int] = z.fold(_ => 1, _ => 0)
}
}
object Akka extends App with Helpers {
override def run(args: List[String]): ZIO[Console, Nothing, Int] = {
(for {
as <- Managed.make(Task(ActorSystem("fabio-akka-test")))(sys => Task.fromFuture(_ => sys.terminate()).ignore).use {
actorSystem =>
implicit val system: ActorSystem = actorSystem
implicit val materializer: ActorMaterializer = ActorMaterializer()
implicit val executionContext: ExecutionContextExecutor = system.dispatcher
val route =
path("hello") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
val tt = Http().bindAndHandle(route, "0.0.0.0", 8080)
ZIO.fromFuture(executionContext => tt).forever
}
} yield 0).exited
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment