Engines

To run a Ktor server application, you need to create and configure a server first. Server configuration can include different settings: a server engine, various engine-specific options, host and port values, and so on. The following engines are supported:

  • Netty

  • Jetty

  • Tomcat

  • CIO (Coroutine-based I/O)

In addition to the engines mentioned above, Ktor provides a special engine type TestEngine for testing application logic. You can learn more about it from Testing.

Add Dependencies

Before using the desired engine, you need to add a corresponding dependency to your build.gradle or pom.xml file:

  • ktor-server-netty
  • ktor-server-jetty
  • ktor-server-tomcat
  • ktor-server-cio

Below are examples of adding a dependency for Netty:

Engines - 图1

Gradle (Groovy)

Gradle (Kotlin)

Maven

Engines - 图2

  1. implementation "io.ktor:ktor-server-netty:$ktor_version"

tip

Engines - 图3

For testing, you need to add the ktor-server-test-host dependency. There is also the ktor-server-servlet dependency that allows you to run an application in a servlet container like Jetty or Tomcat. Learn more at Containers.

Configure Engines

A Ktor server application can be run in two ways:

  • embeddedServer is a simple way to configure server parameters in code and quickly run an application.

  • EngineMain provides more flexibility to configure a server. You can specify server parameters in an application.conf file and change a configuration without recompiling your application. Moreover, you can run your application from a command line and override the required server parameters by passing corresponding command-line arguments.

You can learn more about configuring Ktor from Configurations.

embeddedServer

The embeddedServer function accepts an engine factory used to create an engine of a specific type. In the example below, we pass the Netty factory to run a server with the Netty engine and listen on the 8000 port:

  1. import io.ktor.server.netty.*
  2. fun main() {
  3. embeddedServer(Netty, port = 8000) {
  4. routing {
  5. get ("/") {
  6. call.respondText("Hello, world!")
  7. }
  8. }
  9. }.start(wait = true)
  10. }

EngineMain

EngineMain represents an engine for running a server. You can use the following engines:

  • io.ktor.server.netty.EngineMain
  • io.ktor.server.jetty.EngineMain
  • io.ktor.server.tomcat.EngineMain
  • io.ktor.server.cio.EngineMain

The EngineMain.main function is used to start a server with the selected engine and can accept command-line server parameters. In the example below, we start a server from the application’s main function:

  1. fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
  2. fun Application.module(testing: Boolean = false) {
  3. routing {
  4. get("/") {
  5. call.respondText("Hello, world!")
  6. }
  7. }
  8. }

If you need to start a server using a build system task, you need to configure the required EngineMain as the main class:

Engines - 图4

Gradle (Groovy)

Gradle (Kotlin)

Maven

Engines - 图5

  1. mainClassName = "io.ktor.server.netty.EngineMain"