Browse Source

[scala/zio-http] updated to the latest RC version (#9325)

* [zio-http] updated to the latest RC version

* updated build.sbt, Main.scala
Eddy Oyieko 10 months ago
parent
commit
9c41401165

+ 8 - 7
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)
+    }
   )

+ 1 - 1
frameworks/Scala/zio-http/project/build.properties

@@ -1 +1 @@
-sbt.version = 1.5.5
+sbt.version = 1.10.0

+ 1 - 1
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")

+ 43 - 42
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
+}