Getting Started

Creating a simple HTTP server using http4s and ZIO is as easy as this:

build.sbt

libraryDependencies += "com.avast" %% "sst-bundle-zio-http4s-blaze" % "0.18.4"

Main

import cats.effect.*
import com.avast.sst.http4s.client.*
import com.avast.sst.http4s.server.*
import com.avast.sst.jvm.execution.ExecutorModule
import com.avast.sst.jvm.system.console.ConsoleModule
import org.http4s.dsl.Http4sDsl
import org.http4s.HttpRoutes
import zio.interop.catz.*
import zio.interop.catz.implicits.*
import zio.{Task, ZEnv, Runtime}

implicit val runtime: Runtime[ZEnv] = zio.Runtime.default // this is just needed in example

val dsl = Http4sDsl[Task] // this is just needed in example
import dsl.*

val routes = Http4sRouting.make { 
  HttpRoutes.of[Task] {
    case GET -> Root / "hello" => Ok("Hello World!")
  }
}

val resource = for {
  executorModule <- ExecutorModule.makeDefault[Task]
  console = ConsoleModule.make[Task]
  server <- Http4sBlazeServerModule.make[Task](Http4sBlazeServerConfig("127.0.0.1", 0), routes, executorModule.executionContext)
  client <- Http4sBlazeClientModule.make[Task](Http4sBlazeClientConfig(), executorModule.executionContext)
} yield (server, client, console)

val program = resource
  .use {
    case (server, client, console) =>
      client
        .expect[String](s"http://127.0.0.1:${server.address.getPort}/hello")
        .flatMap(console.printLine)
  }
runtime.unsafeRun(program)
// Hello World!

Or you can use the official Giter8 template:

sbt new avast/sst-seed.g8