A Ktor application typically consists of a series of features. You can think of features as functionality
that is injected into the request and response pipeline. Usually, an application would have a series of features such as DefaultHeaders
which add headers to every outgoing
response, Routing
which allows us to define routes to handle requests, etc.
Table of contents:
A feature is installed into the Application by calling the install
function:
fun Application.main() {
install(DefaultHeaders)
install(CallLogging)
install(Routing) {
get("/") {
call.respondText("Hello, World!")
}
}
}
Some common feature such as Routing
come with helper functions, which are defined as extension functions to Application
, making the code
somewhat more fluent. For instance, instead of writing:
install(Routing) {
get("/") {
call.respondText("Hello, World!")
}
}
we could simply write:
routing {
get("/") {
call.respondText("Hello, World!")
}
}
Ktor comes with a number of ready-made features that can be installed into your application:
Some features might need you to add an extra dependency to your project. See the feature pages for more details.
You can develop your own features and reuse them across your Ktor applications. Refer to Advanced Features for more information.