A Ktor Application

Ktor can be used to create a variety of server (and client-side) applications. Whether we want to create a website that serves static and dynamic pages, an HTTP endpoint, a RESTful system, or even microservices, Ktor makes it all possible.

In this section we’re going to cover the basics of what a Ktor Server Application is and how the pieces all fit together. For more advanced topics and to dive deeper into Ktor under the covers, see the How Ktor works topic.

The Simplest Ktor Application

One of Ktor’s goals is to remain as simple as possible and to avoid any kind of magic. The equivalent to a Hello World application in Ktor would be:

  1. fun main(args: Array<String>) {
  2. embeddedServer(Netty, port = 8080) {
  3. routing {
  4. get("/") {
  5. call.respondText("Hello, Ktor!")
  6. }
  7. }
  8. }.start(true)
  9. }

We start by creating an server, in this case using Netty as an Engine, and setting the port to 8080. After this, we define a route to respond to requests made to / with a simple Hello, Ktor! text. Finally we start the server.

In real-world applications, we would most likely organise things a bit differently so that the code remains maintainable as the application grows. In the following sections we’ll dive deeper into how Ktor works and explain some of the concepts we’ve used.

The Request and Response Pipeline

The request and response flow for a Ktor application is represented by the following diagram:

Request and response pipeline

When a request comes in, it is processed by the routing mechanism, which redirects it to the route handler. This route handle consists of our application logic which could be anything from responding with a simple text (like the example above), to processing the input, storing it in a database, etc. Finally, once everything is done, the handler should respond to the request.

In the next section we’ll look Features which is core to all Ktor applications, and provides much of the functionality, including routing!