|
1 | | -import com.github.plokhotnyuk.jsoniter_scala.core._ |
2 | | -import com.github.plokhotnyuk.jsoniter_scala.macros._ |
3 | | -import snunit._ |
| 1 | +import com.github.plokhotnyuk.jsoniter_scala.core.* |
| 2 | +import com.github.plokhotnyuk.jsoniter_scala.macros.* |
| 3 | +import snunit.* |
4 | 4 |
|
5 | | -final case class Message(message: String) |
| 5 | +final case class Message(message: String) derives ConfiguredJsonValueCodec |
6 | 6 |
|
7 | | -object Message { |
8 | | - implicit final val codec: JsonValueCodec[Message] = JsonCodecMaker.make |
9 | | -} |
| 7 | +final val applicationJson = Headers("Content-Type" -> "application/json") |
| 8 | +final val textPlain = Headers("Content-Type" -> "text/plain") |
10 | 9 |
|
11 | | -object Main { |
12 | | - val applicationJson = Seq("Content-Type" -> "application/json") |
13 | | - val textPlain = Seq("Content-Type" -> "text/plain") |
| 10 | +inline def notFound(req: Request) = req.send( |
| 11 | + statusCode = StatusCode.NotFound, |
| 12 | + content = "Not found", |
| 13 | + headers = textPlain |
| 14 | +) |
14 | 15 |
|
15 | | - @inline |
16 | | - private def notFound(req: Request) = req.send( |
17 | | - statusCode = StatusCode.NotFound, |
18 | | - content = "Not found", |
19 | | - headers = textPlain |
20 | | - ) |
21 | | - |
22 | | - def main(args: Array[String]): Unit = { |
23 | | - SyncServerBuilder |
24 | | - .build(req => |
25 | | - if (req.method == Method.GET) { |
26 | | - if(req.target == "/plaintext") |
27 | | - req.send( |
28 | | - statusCode = StatusCode.OK, |
29 | | - content = "Hello, World!", |
30 | | - headers = textPlain |
31 | | - ) |
32 | | - else if(req.target == "/json") |
33 | | - req.send( |
34 | | - statusCode = StatusCode.OK, |
35 | | - content = writeToArray(Message("Hello, World!")), |
36 | | - headers = applicationJson |
37 | | - ) |
38 | | - else notFound(req) |
39 | | - } else notFound(req) |
40 | | - ) |
41 | | - .listen() |
42 | | - } |
43 | | -} |
| 16 | +@main |
| 17 | +def main = |
| 18 | + SyncServerBuilder |
| 19 | + .setRequestHandler(req => |
| 20 | + if (req.method == Method.GET) { |
| 21 | + if(req.target == "/plaintext") |
| 22 | + req.send( |
| 23 | + statusCode = StatusCode.OK, |
| 24 | + content = "Hello, World!", |
| 25 | + headers = textPlain |
| 26 | + ) |
| 27 | + else if(req.target == "/json") |
| 28 | + req.send( |
| 29 | + statusCode = StatusCode.OK, |
| 30 | + content = writeToArray(Message("Hello, World!")), |
| 31 | + headers = applicationJson |
| 32 | + ) |
| 33 | + else notFound(req) |
| 34 | + } else notFound(req) |
| 35 | + ) |
| 36 | + .build() |
| 37 | + .listen() |
0 commit comments