Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions frameworks/Scala/zio-http/build.sbt
Original file line number Diff line number Diff line change
@@ -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)
}
)
2 changes: 1 addition & 1 deletion frameworks/Scala/zio-http/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.5.5
sbt.version = 1.10.0
2 changes: 1 addition & 1 deletion frameworks/Scala/zio-http/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.0.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.0")
85 changes: 43 additions & 42 deletions frameworks/Scala/zio-http/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -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
}
Loading