What is an Application?

    Ktor also provides functionality to handle raw sockets, but not as part of the Application andits pipeline.

    Table of contents:

    An instance is the main unit of a Ktor Application. When a request comes in(a request can be HTTP, HTTP/2 or WebSocket requests), it is converted to an ApplicationCalland goes through a pipeline which is owned by the Application. The pipeline consists of one or moreinterceptors that are previously installed, providing certain functionality such as routing,compression, etc. that ends handling the request.

    Normally, a Ktor program configures the Application pipeline through that install and configure features.

    Check the .

    A feature is a singleton (usually a companion object) that you can install and configure for a pipeline.Ktor includes some standard features, but you can add your own or other features from the community. You can install features in any pipeline, like the application itself, or specific routes.

    You can read more about features in its dedicated page.

    A Ktor module is just a user-defined function receiving the Application class that is in charge of configuringthe server pipeline, install features, registering routes, handling requests, etc.

    A simple module function would look like this:

    Main.kt

    Of course, you can split the module function in several smaller functions or classes.

    Modules are referenced by their fully qualified name: the fully qualified name of the class and the method name,separated by a dot ().

    1. com.example.myapp.MainKt.mymodule

    mymodule is an extension method of the class Application (where Application is the receiver).Since it is defined as a top-level function, Kotlin creates a JVM class with a suffix (FileNameKt),and adds the extension method as a static method with the receiver as its first parameter.In this case, the class name is MainKt in the com.example.myapp package, and the Java method signature would bestatic public void mymodule(Application app).