Ktor is a Kotlin framework for connected applications. It supports server-side applications and network requests from clients and mobile applications, while using Kotlin features to keep configuration expressive. section 1.3.1
Listing 1.3 A Ktor app uses a DSL to route HTTP requests
fun main() {
embeddedServer(Netty, port = 8000) {
routing { #1
get ("/world") { #2
call.respondText("Hello, world!")
}
route("/greet") {
get { /* . . . */ }
post("/{entityId}") { /* . . . */ } #3
}
}
}.start(wait = true)
}
A compact Ktor server starts an embedded Netty server, opens a routing DSL, returns text from /world, and nests GET and POST handlers under /greet. section 1.3.1
The example highlights three practical ideas:
- embeddedServer starts the application with Netty on port 8000. section 1.3.1
- routing and route organize HTTP endpoints through a Kotlin DSL rather than annotations or XML. section 1.3.1
- get and post declare handlers for different HTTP operations, including a path parameter such as {entityId}. section 1.3.1
