Play Framework:如何获取当前端口号

时间:2015-06-17 04:14:06

标签: scala playframework

如何在Play应用程序中获取当前端口号?我使用scala。

1 个答案:

答案 0 :(得分:0)

以下是我的解决方案:

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)
  }
}

我希望它有所帮助。