Using Javalin.create().port(portNumber)
sets the listen port, but it's unclear how to set the listen/bind address.
Asked
Active
Viewed 1,527 times
5

karmakaze
- 34,689
- 1
- 30
- 32
-
1On a related note... The [github sample code here](https://github.com/tipsy/javalin-http2-example) helped me a great deal with similar questions relating to Jetty config for Javalin, including how to configure HTTP2 and TLS 1.3. – andrewJames Feb 13 '20 at 22:20
2 Answers
3
Found out that you can create the Jetty Server instance yourself and configure it. In Kotlin:
val port = Integer.parseInt(System.getProperty("PORT", "8080"))
val jettyServer = JettyServerUtil.defaultServer()
jettyServer.apply {
connectors = arrayOf(ServerConnector(jettyServer).apply {
this.host = System.getProperty("HOST", "0.0.0.0")
this.port = port
})
}
val app = Javalin.create()
.port(port)
.server { jettyServer }
.start()

karmakaze
- 34,689
- 1
- 30
- 32
2
Here's how to do the same in Java:
int port = Integer.parseInt(System.getProperty("PORT", "8080"))
org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server();
ServerConnector connector = new ServerConnector(server);
connector.setHost(System.getProperty("HOST", "0.0.0.0"));
connector.setPort(port);
server.setConnectors(new ServerConnector[] { connector });
Javalin app = Javalin.create(config -> {
config.server(() -> server);
}).start(port);
Reference: https://github.com/tipsy/javalin/issues/138

karmakaze
- 34,689
- 1
- 30
- 32