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.

Revisions

  1. serragnoli created this gist Oct 28, 2019.
    36 changes: 36 additions & 0 deletions AkkaZio.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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
    }