Modules
note
This help topic is in development and will be updated in the future.
A Ktor Application consists of a series of one or more modules, each of which can house any kind of functionality.
A Ktor module is just a user-defined function receiving the Application
class that is in charge of configuring the server pipeline, install features, registering routes, handling requests, etc.
You have to specify the modules to load when the server starts in the application.conf
file.
A simple module function would look like this:
package com.example.myapp
fun Application.mymodule() {
routing {
get("/") {
call.respondText("Hello World!")
}
}
}
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 (.
).
So for the example, the module’s fully qualified name would be:
com.example.myapp.MainKt.mymodule
note
mymodule
is an extension method of the classApplication
(whereApplication
is the receiver). Since it is defined as a top-level function, Kotlin creates a JVM class with aKt
suffix (FileNameKt
), and adds the extension method as a static method with the receiver as its first parameter. In this case, the class name isMainKt
in thecom.example.myapp
package, and the Java method signature would bestatic public void mymodule(Application app)
.