Skip to content

Instantly share code, notes, and snippets.

@adamw
Created June 2, 2020 06:15
Show Gist options
  • Save adamw/957e8189489a939229d23e5902219903 to your computer and use it in GitHub Desktop.
Save adamw/957e8189489a939229d23e5902219903 to your computer and use it in GitHub Desktop.
package sttp.tapir.examples
import org.http4s._
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.syntax.kleisli._
import zio.interop.catz._
import zio.{Has, RIO, Runtime, ZIO, ZLayer}
import org.http4s.dsl.Http4sDsl
import zio.clock.Clock
object ZioExampleHttp4sServer2 extends App {
type MyEnv = Clock with Has[Int]
type Eff[X] = RIO[MyEnv, X]
val dsl = Http4sDsl[Eff]
import dsl._
val service: HttpRoutes[Eff] = HttpRoutes.of[Eff] {
case GET -> Root / "hello" / name => Ok(RIO.access[Has[Int]](c => s"Hello, $name (${c.get})."))
}
val program: Eff[Unit] = ZIO.runtime[MyEnv].flatMap { implicit runtime =>
BlazeServerBuilder[Eff](runtime.platform.executor.asEC)(taskEffectInstance[MyEnv], zioTimer)
.bindHttp(8080, "localhost")
.withHttpApp(Router("/" -> service).orNotFound)
.serve
.compile
.drain
}
Runtime.default.unsafeRun(program.provideLayer(Clock.live ++ ZLayer.succeed(42)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment