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:
Gradle (Groovy)
Gradle (Kotlin)
Maven
implementation "io.ktor:ktor-server-netty:$ktor_version"
tip
For testing, you need to add the
ktor-server-test-host
dependency. There is also thektor-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:
import io.ktor.server.netty.*
fun main() {
embeddedServer(Netty, port = 8000) {
routing {
get ("/") {
call.respondText("Hello, world!")
}
}
}.start(wait = true)
}
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:
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
fun Application.module(testing: Boolean = false) {
routing {
get("/") {
call.respondText("Hello, world!")
}
}
}
If you need to start a server using a build system task, you need to configure the required EngineMain
as the main class:
Gradle (Groovy)
Gradle (Kotlin)
Maven
mainClassName = "io.ktor.server.netty.EngineMain"