Deploying local Ktor services

To test our new service, we can easily deploy that service locally and validate its function. The local deployment of the service is configured through the application.conf configuration file that was generated when we created our project. That file should look something like the following code snippet:

// application.conf
ktor {
deployment {
port = 8080
port = ${?PORT}
}
application {
modules = [ com.packt.ApplicationKt.module ]
}
}

In this file, there are two key things:

  1. We define the local port to which our service will be deployed and be available.
  2. We define module as our static entry point with which to begin running our service.

The reference to ApplicationKt.module corresponds to the Application.module extension function we've defined in Application.kt:

@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
...
}

With this configuration, when we call our main() function, the Application.module() function will be run and available locally atĀ http://0.0.0.0:8080.

To actually deploy the service, open Application.kt within IntelliJ, and then click the green arrow in the left gutter of the IDE window:

Once this has been clicked, the service will be deployed using Jetty, which will log the deployment status to the run window in IntelliJ. If everything is successful, you should see output similar to the following:

[main] INFO Application - Responding at http://0.0.0.0:8080

Now that our service is deployed, if we openĀ http://0.0.0.0:8080 in our browser, we should see Hello World printed out to the screen as our endpoint response:

And with that, we've now completed the process of creating a simple service in Kotlin, and then deploying that service locally to test and potentially build upon. In the next section, we'll expand on this service by adding additional routes and taking advantage of Kotlin language features to make our service easier to build and maintain.