Play2-mini and Akka2 for HTTP Gateway

I appreciate the possibility of using Play2-mini with Scala to create a service that will be placed between the mobile client and the existing web service. I am looking for the simplest code example in which Play2-mini implements a server and client. Ideally, the client will use acc2 actors.

With this question, I'm trying to figure out how to do this, but also to see how the Play2-Mini and Akka2 should interact. Since Play2-Mini is a replacement for Akka HTTP modules.

Play2-mini contains the following code example in which I created two TODOs. If someone can help me with some sample code to get started, I will be very grateful.

package com.example import com.typesafe.play.mini._ import play.api.mvc._ import play.api.mvc.Results._ object App extends Application { def route = { case GET(Path("/testservice")) & QueryString(qs) => Action{ request=> println(request.body) //TODO Take parameter and content from the request them pass it to the back-end server //TODO Receive a response from the back-end server and pass it back as a response Ok(<h1>Server response: String {result}</h1>).as("text/html") } } } 
+8
scala playframework play2-mini
source share
2 answers

Here is the implementation of your example.

Add the following importers:

 import play.api.libs.ws.WS import play.api.mvc.BodyParsers.parse import scala.xml.XML 

Add the following route:

 case GET(Path("/testservice")) & QueryString(qs) => Action{ request => Async { val backendUrl = QueryString(qs,"target") map (_.get(0)) getOrElse("http://localhost:8080/api/token") val tokenData = QueryString(qs,"data") map (_.get(0)) getOrElse("<auth>john</auth>") WS.url(backendUrl).post(XML loadString tokenData).map { response => Ok(<html><h1>Posted to {backendUrl}</h1> <body> <div><p><b>Request body:</b></p>{tokenData}</div> <div><p><b>Response body:</b></p>{response.body}</div> </body></html>).as("text/html") } } } 

All it does is forward a GET request to server serivce as a POST request. The internal service is specified in the request parameter as target , and the body of the POST request is specified in the request parameter as data (XML must be valid). As a bonus, the request is processed asynchronously (therefore, Async ). After receiving a response from the internal service, the external service responds to some basic HTML showing the response of the back-end service.

If you want to use the request body, I would suggest adding the following POST route, not GET (again, there must be valid XML in this tag):

 case POST(Path("/testservice")) & QueryString(qs) => Action(parse.tolerantXml){ request => Async { val backendUrl = QueryString(qs,"target") map (_.get(0)) getOrElse("http://localhost:8080/api/token") WS.url(backendUrl).post(request.body).map { response => Ok(<html><h1>Posted to {backendUrl}</h1> <body> <div><p><b>Request body:</b></p>{request.body}</div> <div><p><b>Response body:</b></p>{response.body}</div> </body></html>).as("text/html") } } } 

So, as you can see, for your HTTP gateway you can use Async and play.api.libs.ws.WS with Akka under the hood, working to provide asynchronous processing (without explicit Actors). Good luck with your Play2 / Akka2 project.

+7
source

Great answer from romusz

Another way to make a (blocking) HTTP GET request:

 import play.api.libs.ws.WS.WSRequestHolder import play.api.libs.ws.WS.url import play.api.libs.concurrent.Promise import play.api.libs.ws.Response val wsRequestHolder: WSRequestHolder = url("http://yourservice.com") val promiseResponse: Promise[Response] = wsRequestHolder.get() val response = promiseResponse.await.get println("HTTP status code: " + response.status) println("HTTP body: " + response.body) 
+1
source

All Articles