zio-http (ZIO 2.x) application not starting with Scala 3

1.2k views Asked by At

I have this simple application:

import zhttp.http.*
import zhttp.http.Method.GET
import zhttp.service.Server
import zio.*
object HexAppApplication extends ZIOAppDefault {

  // Create HTTP route
  val app: HttpApp[Any, Nothing] = Http.collect[Request] {
    case GET -> !! / "text" => Response.text("Hello World!")
    case GET -> !! / "json" => Response.json("""{"greetings": "Hello World!"}""")
  }

  val program: URIO[Any, ExitCode] = Server.start(8090, app).exitCode

  override def run: URIO[Any, ExitCode] = program

}

The server starts and stops right away. Why doesn't it stay started?

build.sbt

val zioVersion = "2.0.0-RC5"
val zioHttpVersion = "2.0.0-RC6"

libraryDependencies += "dev.zio" %% "zio" % zioVersion
libraryDependencies ++= Seq(
  "dev.zio" %% "zio-test"          % zioVersion % "test",
  "dev.zio" %% "zio-test-sbt"      % zioVersion % "test",
  "dev.zio" %% "zio-test-magnolia" % zioVersion % "test" // optional
)
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")

libraryDependencies += "io.d11" %% "zhttp"      % zioHttpVersion
libraryDependencies += "io.d11" %% "zhttp-test" % zioHttpVersion % Test

The sample from the documentation (for Scala 2.x) looks similar:

import zio._
import zhttp.http._
import zhttp.service.Server

object HelloWorld extends App {
  val app = Http.collect[Request] {
    case Method.GET -> !! / "text" => Response.text("Hello World!")
  }

  override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
    Server.start(8090, app).exitCode
}
2

There are 2 answers

0
John Hungerford On BEST ANSWER

Try updating zioHttpVersion to "2.0.0-RC7". That fixed it for me.

0
Maciej Flak On

It seems that the problem was that zio-http was build against zio in version v2.0.0-RC6 so it is not yet usable with final 2.0.0 nor anything below v2.0.0-RC6 [version] (https://discordapp.com/channels/629491597070827530/819703129267372113/995509195015209000). The support for new final zio release has been just merged so no one should run into this again link to github PR