7

The question is: how do I retrieve the port number that play is listening on, no matter how it was defined (configuration, command line argument, or not at all).

This answer Retrieving port number in-application using Play Framework must be for Play v1 because that code doesn't compile.

In Play 2 Java there is:

Integer port = Play.application().configuration().getInt("http.port");

That doesn't return the actual port number, at least not in all cases. If Play is run as:

run 80 -Dhttp.address=example.com

Then it returns null.

If Play is run as

run -Dhttp.port 80 -Dhttp.address=example.com

Then Play starts on the default port (9000).

As biesior pointed out it works by mentioning the port twice:

play -Dhttp.port=80 "run 80"

Which of course is not optimal since one is what Play actually uses, and the other is what it reports.

But it would answer my question for dev mode. However, as the documentation says, Play should never be run using run in prod. So what's the equivalent for the start command, and is there no better, safer way? (I'm interested in the Java version but others might like to know about Scala too.)

Community
  • 1
  • 1
Gonfi den Tschal
  • 1,754
  • 1
  • 20
  • 29
  • Just a note... You really shouldn't run Play on port 80 (at least on Unix). Because this requires you to run Play as root and that is not a safe thing to do. – James Ward Mar 04 '13 at 04:55

4 Answers4

3

Running in dev mode you need to use doubled notation

play -Dhttp.port=80 "run 80"

Unfortunately you have to do the same for default port

play -Dhttp.port=9000 "run 9000"

So I just suggest to write shell script (or .bat in Windows) for easy starting with all required params.

biesior
  • 55,576
  • 10
  • 125
  • 182
0

I'm using this Scala snippet to determine Play's listen port. Unfortunately I had to hard code some default values, in case -Dhttp.port=... is not specified.

val listenPort =
  if (!Play.isTest) {
    System.getProperty("http.port", "9000")
  }
  else {
    // Not on classpath: play.api.test.Helpers.testServerPort
    // Instead, duplicate its implementation here:
    System.getProperty("testserver.port", "19001")
  }

I also wonder if there is no better way, e.g. Play.port but I haven't found anything — well except for your question :-)

(In your example, it should be -Dhttp.port=80 not -Dhttp.port 80.)

KajMagnus
  • 11,308
  • 15
  • 79
  • 127
0

I just found a way to get the port no matter how the port is specified. Use the reverse route to get an Call object, and the use the absoluteUrl() method to get the absolute url of your action, which contains port. Code snippet:

public class RouteController extends Controller {
public  Promise<Result> getPortAction() {
    Call call = com.mycom.routes.RouteController.getPortAction();
    String absoluteUrl = call.absoluteURL(request());
    //Here absoluteUrl would be like http://localhost:9002/...., 9002 would be the port.
    ...
} }
Jacob Wu
  • 9
  • 1
  • 1
    I'll upvote your response if you can refine this code sample. As you imply, it's best _not_ to interrogate the config. when seeking the port number. In principle, yours is the ideal answer, but I wasn't able to get it working. – Michael Ahlers Mar 13 '16 at 17:16
  • This does not help at all as the "absoluteURL()" method only actually computes the local path section of the URI. Server and port are either omitted or set to whatever the parameters of "absoluteURL()" contained. – Dirk Hillbrecht Oct 16 '16 at 12:27
0

As of play2.8.x, looks like we can use RequestHeader.host https://www.playframework.com/documentation/2.8.x/api/java/play/mvc/Http.RequestHeader.html#host-- And together with RequestHeader.secure from another question play framework2: find out if an app is running on http or https we can even also determine if it's https or http

Steven Lai
  • 138
  • 1
  • 9