Building Complex Servers
Table of contents:
To get started with Ktor, you can start with an embeddedServer
in a simple main
function.
This works fine to understand how Ktor works and to have all the application code availableat a glance.
Once your code starts to grow, and you have more routes defined, you will probably want to splitthe code up instead of growing your main function indefinitely.
A simple way to do this, is to extract routes into extension methods using the Routing
class as a receiver.
Depending on the size, maybe still keeping it in the same file or you can move it to other files:
Once you want to deploy your server, you might also want to provide or to change the configuration of the serverexternally without recompiling it.
Ktor libraries expose some entrypoints that read an file from the resources, or by an externalfile. In this file you can define things like the entry point of the application, the port used, the ssl configurationor arbitrary configurations.
You can read more about using application.conf
in the .
You can also use the StatusPages feature to handle exceptions.