Modules
Ktor allows you to use modules to structure your application by defining a specific set of routes inside a specific module. A module is an extension function of the Application class. In the example below, the module1
extension function defines a module that accepts GET requests made to the /module1
URL path.
Loading modules in your application depends on the way used to create a server: in code using the embeddedServer
function or by using the application.conf
configuration file.
embeddedServer
Typically, the embeddedServer
function accepts a module implicitly as a lambda argument. You can see the example in the embeddedServer section.
If you want to load a module defined as an extension function, use one of the ways described below.
Load a single module
To load a single module, pass its name in the module
parameter.
You can find the full example here: embedded-server-modules.
Load multiple modules
To use multiple modules with embeddedServer
, call the required extension functions inside the block.
HOCON file
If you use the application.conf
file to configure a server, you need to specify modules to load using the ktor.application.modules
property.
Suppose you have three modules defined in two packages: two modules in the com.example
package and one in the org.sample
package.
To reference these modules in a configuration file, you need to provide their fully qualified names, separated by a comma. A fully qualified module name includes a fully qualified name of the class and an extension function name.
You can find the full example here: engine-main-modules.