How do I get the current port number in a Play application? I use scala.
Asked
Active
Viewed 903 times
2
-
You tried this?: http://stackoverflow.com/questions/14970677/retrieving-port-number-in-play-framework-2-app – Identity1 Jun 17 '15 at 06:49
-
1What is version of Playframework you use? – Mon Calamari Jun 17 '15 at 08:43
-
I'm using Play 2.3.8 – j3d Jun 17 '15 at 17:01
-
Yes... I've seen the post mentioned by Identity1... but it doesn't help. I'm looking for a more robust solution. – j3d Jun 17 '15 at 17:02
-
..What is more robust than pulling the port from the app configuration? – Michael Zajac Jun 17 '15 at 23:29
1 Answers
1
Here below is my solution:
def localHost()(implicit request: RequestHeader = null): Host = {
def loadFromConfig = {
val ssl = config.getBoolean("ssl").getOrElse(false)
val host = config.getString("host").getOrElse(EndPoint.DefaultHostName)
val port = Play.isTest match {
case false => System.getProperty("http.port", null) match {
case port if port != null => parse[Int](port).getOrElse(EndPoint.DefaultPort)
case _ => EndPoint.DefaultPort
}
case _ => System.getProperty("testserver.port", null) match {
case port if port != null => parse[Int](port).getOrElse(EndPoint.DefaultTestPort)
case _ => EndPoint.DefaultTestPort
}
}
Host(EndPoint(host, port), ssl)
}
request match {
case null => if (_localHost == null) this.synchronized {
if (_localHost == null) _localHost = loadFromConfig
}
_localHost
case _ => Host(EndPoint(Some(request.host)), request.secure)
}
}
I hope it helps.
j3d
- 9,492
- 22
- 88
- 172