Skip to content

Commit dbb0b11

Browse files
committed
[zio-http] updated to the latest RC version
1 parent 8d4f5ed commit dbb0b11

File tree

4 files changed

+51
-50
lines changed

4 files changed

+51
-50
lines changed

frameworks/Scala/zio-http/build.sbt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ version := "1.0.0"
33
scalaVersion := "2.13.6"
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: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
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+
6+
object Main extends ZIOAppDefault {
7+
8+
private val plainTextMessage: String = "hello, world!"
9+
private val jsonMessage: String = """{"message": "hello, world!"}"""
10+
11+
private val STATIC_SERVER_NAME = "zio-http"
12+
private val NUM_PROCESSORS = Runtime.getRuntime.availableProcessors()
13+
14+
val app: Routes[Any, Response] = Routes(
15+
Method.GET / "/plaintext" ->
16+
Handler.fromResponse(
17+
Response
18+
.text(plainTextMessage)
19+
.addHeader(Header.Server(STATIC_SERVER_NAME)),
20+
),
21+
Method.GET / "/json" ->
22+
Handler.fromResponse(
23+
Response
24+
.json(jsonMessage)
25+
.addHeader(Header.Server(STATIC_SERVER_NAME)),
26+
),
27+
)
28+
29+
private val config = Server.Config.default
30+
.port(8080)
31+
.enableRequestStreaming
32+
33+
private val nettyConfig = NettyConfig.default
34+
.leakDetection(LeakDetectionLevel.DISABLED)
35+
.maxThreads(NUM_PROCESSORS)
36+
37+
private val configLayer = ZLayer.succeed(config)
38+
private val nettyConfigLayer = ZLayer.succeed(nettyConfig)
39+
40+
val run: UIO[ExitCode] =
41+
Server.serve(app.toHttpApp).provide(configLayer, nettyConfigLayer, Server.customized).exitCode
42+
}

0 commit comments

Comments
 (0)