Skip to content

Commit

Permalink
ZIO example
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed May 7, 2019
1 parent 28152f8 commit 7144ff0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,12 @@ lazy val playground: Project = (project in file("playground"))
name := "tapir-tests",
libraryDependencies ++= Seq(
"com.softwaremill.sttp" %% "akka-http-backend" % sttpVersion,
"org.scalaz" %% "scalaz-zio" % "1.0-RC4",
"org.scalaz" %% "scalaz-zio-interop-cats" % "1.0-RC4",
"org.typelevel" %% "cats-effect" % "1.2.0",
"org.webjars" % "swagger-ui" % "3.22.0"
),
libraryDependencies ++= loggerDependencies,
publishArtifact := false
)
.dependsOn(akkaHttpServer, sttpClient, openapiCirceYaml, openapiDocs, circeJson)
.dependsOn(akkaHttpServer, http4sServer, sttpClient, openapiCirceYaml, openapiDocs, circeJson)
39 changes: 39 additions & 0 deletions playground/src/main/scala/tapir/example/zio/ZioExample.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tapir.example.zio

import tapir._
import tapir.server.http4s._
import org.http4s._
import org.http4s.syntax.kleisli._
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import scalaz.zio.{DefaultRuntime, IO, Task, UIO}
import scalaz.zio.interop.catz._
import scalaz.zio.interop.catz.implicits._

object ZioExample extends App {

implicit class ZioEndpoint[I, E, O](e: Endpoint[I, E, O, EntityBody[Task]]) {
def toZioRoutes(logic: I => IO[E, O])(implicit serverOptions: Http4sServerOptions[Task]): HttpRoutes[Task] = {
import tapir.server.http4s._
e.toRoutes(i => logic(i).either)
}
}

def countCharacters(s: String): UIO[Int] = UIO.succeed(s.length)

val countCharactersEndpoint: Endpoint[String, Unit, Int, Nothing] = endpoint.in(stringBody).out(plainBody[Int])
def service: HttpRoutes[Task] = countCharactersEndpoint.toZioRoutes(countCharacters _)

{
implicit val runtime: DefaultRuntime = new DefaultRuntime {}

val serve = BlazeServerBuilder[Task]
.bindHttp(8080, "localhost")
.withHttpApp(Router("/" -> service).orNotFound)
.serve
.compile
.drain

runtime.unsafeRun(serve)
}
}

0 comments on commit 7144ff0

Please sign in to comment.