Created
December 4, 2012 19:20
-
-
Save mikesname/4207707 to your computer and use it in GitHub Desktop.
Basic web proxy with Play 2.0 - example only and not for actual use...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
app/controllers/MyProxyController.scala | |
*/ | |
package controllers | |
// Basic proxying web service... | |
// Standard play libs - all the Scala stuff | |
// is in the play.api package so it doesn't | |
// conflict with the Java equivalents. | |
import play.api._ | |
import play.api.mvc._ | |
// Import the ws package | |
import play.api.libs.ws | |
// This stuff is needed to configure the asyncronous | |
// stuff which operates via a worker pool | |
import play.api.libs.concurrent.execution.defaultContext | |
import scala.concurrent.Future | |
// Controllers are (usually) singleton objects in Scala, rather than classes | |
object MyProxyController extends Controller { | |
// Take an url, like http://localhost:7474/db/data/, call it, | |
// and return the results as json | |
def proxy(url: String) = Action { | |
// 'Async' is a wrapper that allows you to return an asyncronous | |
// response as if it were a regular one... | |
Async { | |
// The WS lib is asyncronous, so it returns a 'future' web | |
// service response. | |
// The 'get' is a us calling GET, as opposed to POST or DELETE | |
val futureResult: Future[ws.Response] = ws.WS.url(url).get | |
// To specify what we'd like to do when the 'future' is redeemed | |
// we use its 'map' function (lots of things in Scala have a map | |
// function, which basically means 'take whats inside of this and | |
// process it with the given function' | |
futureResult.map { response => | |
// 'Ok' is a Response with a 200 status... we're not bothering | |
// to check for errors in the web service request... | |
Ok(response.json).as("application/json") | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Routes (conf/routes) | |
# This file defines all application routes (Higher priority routes first) | |
# ~~~~ | |
GET /proxy controllers.MyProxyController.proxy(url: String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment