I'm evaluating the possibility of using Play2-mini with Scala to develop a service that will sit between a mobile client and existing web service. I'm looking for the simplest possible example of a piece of code where Play2-mini implements a server and a client. Ideally the client will use Akka2 actors.
With this question, I'm trying to find out how it is done, but also to see how Play2-Mini and Akka2 should co-operate. Since Play2-Mini appears to be the replacement for the Akka HTTP modules.
Play2-mini contains the following code example, in which I created two TODO's. If someone can help me with some sample code to get started, I will be really 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")
}
}
}
Here's the implementation of your example.
Add the following imports:
Add the following route:
All it does, is forwarding a
GETrequest to a back-end serivce as aPOSTrequest. The back-end service is specified in the request parameter astargetand the body for the POST request is specified in the request parameter asdata(must be valid XML). As a bonus the request is handled asynchronously (henceAsync). Once the response from the back-end service is received the front-end service responds with some basic HTML showing the back-end service response.If you wanted to use request body, I would suggest adding the following
POSTroute rather thanGET(again, in this implementation body must be a valid XML):So as you can see, for your HTTP Gateway you can use
Asyncandplay.api.libs.ws.WSwith Akka under the hood working to provide asynchronous handling (no explicit Actors required). Good luck with your Play2/Akka2 project.