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.
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:
We start by creating a server, in this case using Netty as an Engine, and setting the port to 8000. After this, we define a route to respond to requests made to / with a simple Hello, world! text. Finally, we start the server.
In the next chapters, we'll take a look at the main concepts of creating a Ktor application - from configuring a server to using plugins, which is core to all Ktor applications:
Choose a way to create a server
You can quickly configure a server using the
embeddedServerfunction, or you can use
EngineMainto have additional configuration possibilities.
Choose an engine
Configuring a server requires choosing an engine, such as Netty, Jetty, Tomcat, and so on.
Configure a server
Learn how to configure various server parameters (such as a host address and port, modules to load, etc.), read the configuration in code, and use environment variables.
Add functionality with Plugins (formerly known as Features)
Get acquainted with plugins that provide common functionality, for example, serialization and content encoding, compression, headers, cookie support, etc.
Handle requests with Routing
Learn how to handle incoming requests made to different URLs using routing.
Divide your application into Modules
Use modules to divide your application into logical parts, each of which can house any functionality.
Structure your application
Learn how to structure your application to keep it maintainable as the application grows.