In play 1.x you had the http.path param which allowed you to set a url to add to every route
http.param
How can I achieve someting similar in play 2.0?
The conf/routes file is the configuration file used by the Router. This file lists all the routes needed by the application.
Changing the default Content-Type Will automatically set the Content-Type header to text/plain , while: JsonNode json = Json. toJson(object); Result jsonResult = ok(json); will set the Content-Type header to application/json .
The routes file syntax conf/routes is the configuration file used by the router. This file lists all of the routes needed by the application. Each route consists of an HTTP method and URI pattern, both associated with a call to an Action generator.
A Router is an actor that routes incoming messages to outbound actors. The router routes the messages sent to it to its underlying actors called 'routees'.
In Play 2.1 you can do that with the following option in conf/application.conf
:
application.context="/your/prefix"
From Play 2.4 this property is called play.http.context
(taken from the comment by Gman).
I asked at play's discussion group and they helped me achieve this initial version
I create a PrefixedRequest like this
import play.api.mvc.RequestHeader
import play.api.Play.configuration
import play.api.Play.current
class PrefixedRequest(request: RequestHeader) extends RequestHeader {
def headers = request.headers
def queryString = request.queryString
// strip first part of path and uri if it matches http.path config
def path = ("^" + prefix).r.replaceFirstIn(request.path, "/")
def uri = ("^" + prefix).r.replaceFirstIn(request.uri, "/")
def method = request.method
def remoteAddress = request.remoteAddress
lazy val prefix = {
val config = configuration.getString("http.path").getOrElse("")
if (config.endsWith("/")) config else config + "/"
}
}
object PrefixedRequest {
def apply(request: RequestHeader) = new PrefixedRequest(request)
}
Then I used it in Global.scala
import play.api.GlobalSettings
import play.api.mvc.RequestHeader
import play.api.mvc.Handler
object Global extends GlobalSettings {
override def onRouteRequest(request: RequestHeader): Option[Handler] = {
super.onRouteRequest(PrefixedRequest(request))
}
}
finnally added this to application.conf
http.path=/prefix/
It seems to work, but I couln't find out how to add that prefix to the reversed routes... can anybody gimme a hand on that part?
--
Some useful links
Check this thread and the docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With