-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Main.scala
43 lines (35 loc) · 1.28 KB
/
Main.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
}