Repository: tipsy/javalin (kotlin)

Issue:
Move default port configuration to `JavalinConfig`
By default, Javalin starts on port ::8080 and there's no option to configure this behavior within the configuration scope.

https://github.com/javalin/javalin/blob/4a6240bf57c161d8f6dc0bea5e2edf20aa05da42/javalin/src/main/java/io/javalin/Javalin.java#L121-L129

It'd be nice, if we could move this property to cfg:

```kotlin
val app = Javalin
  .create(cfg -> {
    cfg.defaultPort = 1234;
  })
  .start();
```

Effectively, we could even simplify this to:

```kotlin
// default ::8080
val app = Javalin.start()

// custom port/other cfg properties
val app = Javalin.start(cfg -> {
  cfg.defaultPort = 1234;
})
```

**Edit**
Same thing with hostname.

Pull request description:
Resolves #2001

Patch:
diff --git a/javalin/src/main/java/io/javalin/Javalin.java b/javalin/src/main/java/io/javalin/Javalin.java
index 9616df5a1..c450fd76c 100644
--- a/javalin/src/main/java/io/javalin/Javalin.java
+++ b/javalin/src/main/java/io/javalin/Javalin.java
@@ -18,7 +18,6 @@
 import io.javalin.jetty.JavalinJettyServlet;
 import io.javalin.jetty.JettyServer;
 import io.javalin.security.RouteRole;
-import io.javalin.util.Util;
 
 import java.util.function.Consumer;
 
@@ -60,7 +59,9 @@ public JettyServer jettyServer() {
 
     /**
      * Creates a new instance without any custom configuration.
+     * The server does not run until {@link Javalin#start()} is called.
      *
+     * @return application instance
      * @see Javalin#create(Consumer)
      */
     public static Javalin create() {
@@ -72,7 +73,7 @@ public static Javalin create() {
      * Creates a new instance with the user provided configuration.
      * The server does not run until {@link Javalin#start()} is called.
      *
-     * @return application instance.
+     * @return application instance
      * @see Javalin#start()
      * @see Javalin#start(int)
      */
@@ -84,6 +85,17 @@ public static Javalin create(Consumer<JavalinConfig> config) {
         return app;
     }
 
+    /**
+     * Creates a new instance with the user provided configuration and starts it immediately.
+     *
+     * @return running application instance
+     * @see io.javalin.Javalin#create(java.util.function.Consumer)
+     * @see Javalin#start()
+     */
+    public static Javalin createAndStart(Consumer<JavalinConfig> config) {
+        return create(config).start();
+    }
+
     // Get JavalinServlet (can be attached to other servlet containers)
     public JavalinServlet javalinServlet() {
         return this.javalinServlet;
@@ -100,7 +112,6 @@ public JavalinServlet javalinServlet() {
      * @see Javalin#start()
      */
     public Javalin start(String host, int port) {
-        Util.printHelpfulMessageIfLoggerIsMissing();
         jettyServer.getValue().start(host, port);
         return this;
     }
@@ -115,7 +126,8 @@ public Javalin start(String host, int port) {
      * @see Javalin#start()
      */
     public Javalin start(int port) {
-        return start(null, port);
+        jettyServer.getValue().start(null, port);
+        return this;
     }
 
     /**
@@ -127,7 +139,8 @@ public Javalin start(int port) {
      * @see Javalin#create()
      */
     public Javalin start() {
-        return start(null, -1);
+        jettyServer.getValue().start(null, null);
+        return this;
     }
 
     /**
diff --git a/javalin/src/main/java/io/javalin/config/JettyConfig.kt b/javalin/src/main/java/io/javalin/config/JettyConfig.kt
index 93e0d909c..736ab67a4 100644
--- a/javalin/src/main/java/io/javalin/config/JettyConfig.kt
+++ b/javalin/src/main/java/io/javalin/config/JettyConfig.kt
@@ -14,6 +14,8 @@ import java.util.function.Consumer
 /**  Configures the embedded Jetty webserver. */
 class JettyConfig(private val cfg: JavalinConfig) {
     //@formatter:off
+    @JvmField var defaultHost: String? = null
+    @JvmField var defaultPort = 8080
     @JvmField val multipartConfig = MultipartConfig()
     @JvmField var threadPool: ThreadPool = JettyServer.defaultThreadPool()
     //@formatter:on
diff --git a/javalin/src/main/java/io/javalin/jetty/JettyServer.kt b/javalin/src/main/java/io/javalin/jetty/JettyServer.kt
index 411312cdf..deb763a67 100644
--- a/javalin/src/main/java/io/javalin/jetty/JettyServer.kt
+++ b/javalin/src/main/java/io/javalin/jetty/JettyServer.kt
@@ -61,6 +61,7 @@ class JettyServer(
 
     @Throws(JavalinException::class)
     fun start(host: String?, port: Int?) {
+        Util.printHelpfulMessageIfLoggerIsMissing()
         if (started) {
             throw JavalinException("Server already started - Javalin instances cannot be reused.")
         }
@@ -80,8 +81,8 @@ class JettyServer(
             cfg.jetty.connectors.map{ it.apply(this, httpConfiguration) }.forEach(this::addConnector) // add user connectors
             if (connectors.isEmpty()) { // add default connector if no connectors are specified
                 connectors = arrayOf(ServerConnector(server, HttpConnectionFactory(httpConfiguration)).apply {
-                    this.host = host
-                    this.port = port ?: 8080
+                    this.host = host ?: cfg.jetty.defaultHost
+                    this.port = port ?: cfg.jetty.defaultPort
                 })
             }
         }


Test patch:
diff --git a/javalin/src/test/java/io/javalin/TestConfiguration.kt b/javalin/src/test/java/io/javalin/TestConfiguration.kt
index fc4c6f024..6928f2d24 100644
--- a/javalin/src/test/java/io/javalin/TestConfiguration.kt
+++ b/javalin/src/test/java/io/javalin/TestConfiguration.kt
@@ -27,7 +27,8 @@ class TestConfiguration {
 
     @Test
     fun `all config options`() = TestUtil.runLogLess {
-        val app = Javalin.create {
+        val app = Javalin.createAndStart {
+            it.jetty.defaultPort = 0
             it.spaRoot.addFile("/", "/public/html.html")
             it.spaRoot.addFile("/", "src/test/resources/public/html.html", Location.EXTERNAL)
             it.spaRoot.addHandler("/", {})
@@ -57,7 +58,7 @@ class TestConfiguration {
             }
             it.jetty.modifyHttpConfiguration() { httpConfig -> }
             it.jetty.addConnector { server, _ -> ServerConnector(server) }
-        }.start(0)
+        }
         assertThat(app.jettyServer().started()).isTrue()
         app.stop()
     }