2

How do I get the current port number in a Play application? I use scala.

j3d
  • 9,492
  • 22
  • 88
  • 172

1 Answers1

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