Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Javalin's Jetty http server to bind/listen on a specific address/port?

Using Javalin.create().port(portNumber) sets the listen port, but it's unclear how to set the listen/bind address.

like image 865
karmakaze Avatar asked Feb 04 '19 04:02

karmakaze


1 Answers

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()
like image 142
karmakaze Avatar answered Sep 28 '22 03:09

karmakaze