diff --git a/frameworks/Scala/zio-http/build.sbt b/frameworks/Scala/zio-http/build.sbt index 987ee8cbbd2..a985d9b5dc9 100644 --- a/frameworks/Scala/zio-http/build.sbt +++ b/frameworks/Scala/zio-http/build.sbt @@ -1,13 +1,14 @@ name := "zio-http" version := "1.0.0" -scalaVersion := "2.13.6" +scalaVersion := "2.13.14" lazy val root = (project in file(".")) .settings( - libraryDependencies ++= - Seq( - "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % "2.9.1", - "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "2.9.1" % "compile-internal", - "io.d11" % "zhttp" % "1.0.0-RC5", - ), + libraryDependencies += "dev.zio" %% "zio-http" % "3.0.0-RC10", testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"), + assembly / assemblyMergeStrategy := { + case x if x.contains("io.netty.versions.properties") => MergeStrategy.discard + case x => + val oldStrategy = (assembly / assemblyMergeStrategy).value + oldStrategy(x) + } ) diff --git a/frameworks/Scala/zio-http/project/build.properties b/frameworks/Scala/zio-http/project/build.properties index 215ddd2b39d..ee06c398644 100644 --- a/frameworks/Scala/zio-http/project/build.properties +++ b/frameworks/Scala/zio-http/project/build.properties @@ -1 +1 @@ -sbt.version = 1.5.5 \ No newline at end of file +sbt.version = 1.10.0 \ No newline at end of file diff --git a/frameworks/Scala/zio-http/project/plugins.sbt b/frameworks/Scala/zio-http/project/plugins.sbt index 585d1930dc6..ec25e7aa776 100644 --- a/frameworks/Scala/zio-http/project/plugins.sbt +++ b/frameworks/Scala/zio-http/project/plugins.sbt @@ -1 +1 @@ -addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.0.0") +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.0") diff --git a/frameworks/Scala/zio-http/src/main/scala/Main.scala b/frameworks/Scala/zio-http/src/main/scala/Main.scala index b72da4e089c..8af102fd7f6 100644 --- a/frameworks/Scala/zio-http/src/main/scala/Main.scala +++ b/frameworks/Scala/zio-http/src/main/scala/Main.scala @@ -1,42 +1,43 @@ -import zhttp.http._ -import zhttp.service.Server -import zio.{App, ExitCode, URIO} -import com.github.plokhotnyuk.jsoniter_scala.macros._ -import com.github.plokhotnyuk.jsoniter_scala.core._ -import zhttp.http.Response - -import java.time.format.DateTimeFormatter -import java.time.{Instant, ZoneOffset} - -case class Message(message: String) - -object Main extends App { - val message: String = "Hello, World!" - implicit val codec: JsonValueCodec[Message] = JsonCodecMaker.make - - val app: Http[Any, HttpError, Request, Response] = Http.collect[Request] { - case Method.GET -> Root / "plaintext" => - Response.http( - content = HttpContent.Complete(message), - headers = Header.contentTypeTextPlain :: headers(), - ) - case Method.GET -> Root / "json" => - Response.http( - content = HttpContent.Complete(writeToString(Message(message))), - headers = Header.contentTypeJson :: headers(), - ) - } - - override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = Server.start(8080, app).exitCode - - val formatter: DateTimeFormatter = DateTimeFormatter.RFC_1123_DATE_TIME.withZone(ZoneOffset.UTC) - val constantHeaders: List[Header] = Header("server", "zio-http") :: Nil - @volatile var lastHeaders: (Long, List[Header]) = (0, Nil) - - def headers(): List[Header] = { - val t = System.currentTimeMillis() - if (t - lastHeaders._1 >= 1000) - lastHeaders = (t, Header("date", formatter.format(Instant.ofEpochMilli(t))) :: constantHeaders) - lastHeaders._2 - } -} +import zio._ +import zio.http._ +import zio.http.netty.NettyConfig +import zio.http.netty.NettyConfig.LeakDetectionLevel +import java.lang.{Runtime => JRuntime} + +object Main extends ZIOAppDefault { + + private val plainTextMessage: String = "hello, world!" + private val jsonMessage: String = """{"message": "hello, world!"}""" + + private val STATIC_SERVER_NAME = "zio-http" + private val NUM_PROCESSORS = JRuntime.getRuntime.availableProcessors() + + val app: Routes[Any, Response] = Routes( + Method.GET / "/plaintext" -> + Handler.fromResponse( + Response + .text(plainTextMessage) + .addHeader(Header.Server(STATIC_SERVER_NAME)), + ), + Method.GET / "/json" -> + Handler.fromResponse( + Response + .json(jsonMessage) + .addHeader(Header.Server(STATIC_SERVER_NAME)), + ), + ) + + private val config = Server.Config.default + .port(8080) + .enableRequestStreaming + + private val nettyConfig = NettyConfig.default + .leakDetection(LeakDetectionLevel.DISABLED) + .maxThreads(NUM_PROCESSORS) + + private val configLayer = ZLayer.succeed(config) + private val nettyConfigLayer = ZLayer.succeed(nettyConfig) + + val run: UIO[ExitCode] = + Server.serve(app).provide(configLayer, nettyConfigLayer, Server.customized).exitCode +} \ No newline at end of file