-
Notifications
You must be signed in to change notification settings - Fork 3
/
HelloWorldController.scala
29 lines (26 loc) · 1.2 KB
/
HelloWorldController.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
package microservice
import akka.util.ByteString
import javax.inject.Singleton
import com.google.inject.Inject
import play.api.mvc._
import scala.concurrent.{ExecutionContext, Future}
@Singleton
class HelloWorldController @Inject() (components: ControllerComponents)
(implicit ec: ExecutionContext) extends AbstractController(components) {
import com.github.plokhotnyuk.jsoniter_scala.core.{writeToArray, readFromArray}
val jsonGet: Action[AnyContent] = components.actionBuilder.async {
Future(Ok(ByteString.fromArrayUnsafe(writeToArray(HelloWorld("Hello, World!")))))
}
val plaintextGet: Action[AnyContent] = components.actionBuilder.async {
Future(Ok("Hello, World!"))
}
val jsonPost: Action[ByteString] = components.actionBuilder.async(parse.byteString) { request =>
if (request.headers.get("Content-Type").contains("application/json; charset=utf-8")) {
val message = readFromArray[HelloWorld](request.body.toArray)
Future(Ok(ByteString.fromArrayUnsafe(writeToArray(message))))
} else Future(NotAcceptable)
}
val plaintextPost: Action[String] = components.actionBuilder.async(parse.tolerantText) { request =>
Future(Ok(request.body))
}
}