Skip to content

Commit 9c41401

Browse files
authored
[scala/zio-http] updated to the latest RC version (#9325)
* [zio-http] updated to the latest RC version * updated build.sbt, Main.scala
1 parent 51155f1 commit 9c41401

File tree

4 files changed

+53
-51
lines changed

4 files changed

+53
-51
lines changed
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
name := "zio-http"
22
version := "1.0.0"
3-
scalaVersion := "2.13.6"
3+
scalaVersion := "2.13.14"
44
lazy val root = (project in file("."))
55
.settings(
6-
libraryDependencies ++=
7-
Seq(
8-
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % "2.9.1",
9-
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "2.9.1" % "compile-internal",
10-
"io.d11" % "zhttp" % "1.0.0-RC5",
11-
),
6+
libraryDependencies += "dev.zio" %% "zio-http" % "3.0.0-RC10",
127
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
8+
assembly / assemblyMergeStrategy := {
9+
case x if x.contains("io.netty.versions.properties") => MergeStrategy.discard
10+
case x =>
11+
val oldStrategy = (assembly / assemblyMergeStrategy).value
12+
oldStrategy(x)
13+
}
1314
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version = 1.5.5
1+
sbt.version = 1.10.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.0.0")
1+
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.0")
Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
1-
import zhttp.http._
2-
import zhttp.service.Server
3-
import zio.{App, ExitCode, URIO}
4-
import com.github.plokhotnyuk.jsoniter_scala.macros._
5-
import com.github.plokhotnyuk.jsoniter_scala.core._
6-
import zhttp.http.Response
7-
8-
import java.time.format.DateTimeFormatter
9-
import java.time.{Instant, ZoneOffset}
10-
11-
case class Message(message: String)
12-
13-
object Main extends App {
14-
val message: String = "Hello, World!"
15-
implicit val codec: JsonValueCodec[Message] = JsonCodecMaker.make
16-
17-
val app: Http[Any, HttpError, Request, Response] = Http.collect[Request] {
18-
case Method.GET -> Root / "plaintext" =>
19-
Response.http(
20-
content = HttpContent.Complete(message),
21-
headers = Header.contentTypeTextPlain :: headers(),
22-
)
23-
case Method.GET -> Root / "json" =>
24-
Response.http(
25-
content = HttpContent.Complete(writeToString(Message(message))),
26-
headers = Header.contentTypeJson :: headers(),
27-
)
28-
}
29-
30-
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = Server.start(8080, app).exitCode
31-
32-
val formatter: DateTimeFormatter = DateTimeFormatter.RFC_1123_DATE_TIME.withZone(ZoneOffset.UTC)
33-
val constantHeaders: List[Header] = Header("server", "zio-http") :: Nil
34-
@volatile var lastHeaders: (Long, List[Header]) = (0, Nil)
35-
36-
def headers(): List[Header] = {
37-
val t = System.currentTimeMillis()
38-
if (t - lastHeaders._1 >= 1000)
39-
lastHeaders = (t, Header("date", formatter.format(Instant.ofEpochMilli(t))) :: constantHeaders)
40-
lastHeaders._2
41-
}
42-
}
1+
import zio._
2+
import zio.http._
3+
import zio.http.netty.NettyConfig
4+
import zio.http.netty.NettyConfig.LeakDetectionLevel
5+
import java.lang.{Runtime => JRuntime}
6+
7+
object Main extends ZIOAppDefault {
8+
9+
private val plainTextMessage: String = "hello, world!"
10+
private val jsonMessage: String = """{"message": "hello, world!"}"""
11+
12+
private val STATIC_SERVER_NAME = "zio-http"
13+
private val NUM_PROCESSORS = JRuntime.getRuntime.availableProcessors()
14+
15+
val app: Routes[Any, Response] = Routes(
16+
Method.GET / "/plaintext" ->
17+
Handler.fromResponse(
18+
Response
19+
.text(plainTextMessage)
20+
.addHeader(Header.Server(STATIC_SERVER_NAME)),
21+
),
22+
Method.GET / "/json" ->
23+
Handler.fromResponse(
24+
Response
25+
.json(jsonMessage)
26+
.addHeader(Header.Server(STATIC_SERVER_NAME)),
27+
),
28+
)
29+
30+
private val config = Server.Config.default
31+
.port(8080)
32+
.enableRequestStreaming
33+
34+
private val nettyConfig = NettyConfig.default
35+
.leakDetection(LeakDetectionLevel.DISABLED)
36+
.maxThreads(NUM_PROCESSORS)
37+
38+
private val configLayer = ZLayer.succeed(config)
39+
private val nettyConfigLayer = ZLayer.succeed(nettyConfig)
40+
41+
val run: UIO[ExitCode] =
42+
Server.serve(app).provide(configLayer, nettyConfigLayer, Server.customized).exitCode
43+
}

0 commit comments

Comments
 (0)