Home > Software engineering >  zio-http (ZIO 2.x) application not starting with Scala 3
zio-http (ZIO 2.x) application not starting with Scala 3

Time:04-22

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
}

CodePudding user response:

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

  • Related