|
@@ -9,29 +9,35 @@ object Message {
|
|
}
|
|
}
|
|
|
|
|
|
object Main {
|
|
object Main {
|
|
|
|
+ val applicationJson = Seq("Content-Type" -> "application/json")
|
|
|
|
+ val textPlain = Seq("Content-Type" -> "text/plain")
|
|
|
|
+
|
|
|
|
+ @inline
|
|
|
|
+ private def notFound(req: Request) = req.send(
|
|
|
|
+ statusCode = StatusCode.NotFound,
|
|
|
|
+ content = "Not found",
|
|
|
|
+ headers = textPlain
|
|
|
|
+ )
|
|
|
|
+
|
|
def main(args: Array[String]): Unit = {
|
|
def main(args: Array[String]): Unit = {
|
|
- val server = SyncServerBuilder
|
|
|
|
|
|
+ SyncServerBuilder
|
|
.build(req =>
|
|
.build(req =>
|
|
- if (req.method == Method.GET && req.path == "/plaintext")
|
|
|
|
- req.send(
|
|
|
|
- statusCode = StatusCode.OK,
|
|
|
|
- content = "Hello, World!",
|
|
|
|
- headers = Seq("Content-Type" -> "text/plain")
|
|
|
|
- )
|
|
|
|
- else if (req.method == Method.GET && req.path == "/json")
|
|
|
|
- req.send(
|
|
|
|
- statusCode = StatusCode.OK,
|
|
|
|
- content = writeToArray(Message("Hello, World!")),
|
|
|
|
- headers = Seq("Content-Type" -> "application/json")
|
|
|
|
- )
|
|
|
|
- else
|
|
|
|
- req.send(
|
|
|
|
- statusCode = StatusCode.NotFound,
|
|
|
|
- content = "Not found",
|
|
|
|
- headers = Seq("Content-Type" -> "text/plain")
|
|
|
|
- )
|
|
|
|
|
|
+ if (req.method == Method.GET) {
|
|
|
|
+ if(req.target == "/plaintext")
|
|
|
|
+ req.send(
|
|
|
|
+ statusCode = StatusCode.OK,
|
|
|
|
+ content = "Hello, World!",
|
|
|
|
+ headers = textPlain
|
|
|
|
+ )
|
|
|
|
+ else if(req.target == "/json")
|
|
|
|
+ req.send(
|
|
|
|
+ statusCode = StatusCode.OK,
|
|
|
|
+ content = writeToArray(Message("Hello, World!")),
|
|
|
|
+ headers = applicationJson
|
|
|
|
+ )
|
|
|
|
+ else notFound(req)
|
|
|
|
+ } else notFound(req)
|
|
)
|
|
)
|
|
-
|
|
|
|
- server.listen()
|
|
|
|
|
|
+ .listen()
|
|
}
|
|
}
|
|
}
|
|
}
|