Ktor Project Setup: embeddedServer and Application Modules
Bootstrap a Ktor application, configure Netty, and define application modules.
Ktor Project Setup: embeddedServer and Application Modules is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Ktor?
Ktor is JetBrains' asynchronous Kotlin framework for building servers and clients. It is coroutine-first, plugin-based, and has no magic: every feature you need is explicitly installed. Ktor applications are plain Kotlin programs — no DI container required.
Adding Ktor Dependencies
Add the core server engine (Netty is the most common) and the core Ktor server library to your build.gradle.kts:
dependencies {
implementation("io.ktor:ktor-server-core:2.3.12")
implementation("io.ktor:ktor-server-netty:2.3.12")
implementation("ch.qos.logback:logback-classic:1.5.6") // logging
}embeddedServer: Minimal Entry Point
The simplest way to start a Ktor server is embeddedServer. Pass the engine factory, port, and a configuration block:
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.routing.*
import io.ktor.server.response.*
fun main() {
embeddedServer(Netty, port = 8080) {
routing {
get("/") { call.respondText("Hello, Ktor!") }
}
}.start(wait = true)
}Application Modules
For larger projects, split configuration into modules — extension functions on Application. A module installs plugins and registers routes. This makes the codebase more testable and composable:
fun Application.configureRouting() {
routing {
get("/health") { call.respondText("OK") }
}
}
fun Application.configureSerialization() {
install(ContentNegotiation) { json() }
}Calling Modules from main
Call your module functions inside the embeddedServer block to compose them:
fun main() {
embeddedServer(Netty, port = 8080) {
configureRouting()
configureSerialization()
configureSecurity()
}.start(wait = true)
}application.conf (HOCON Configuration)
The alternative to embeddedServer is using an application.conf file (HOCON format) and running via EngineMain. The config file specifies the entry module and port:
# application.conf
ktor {
deployment { port = 8080 }
application {
modules = [ com.example.ApplicationKt.module ]
}
}EngineMain vs embeddedServer
Use embeddedServer when you want full control in code (great for testing and simple apps). Use EngineMain + application.conf when you want environment-specific config without recompiling.
The ApplicationCall Object
Every request handler receives an ApplicationCall. It provides access to the request (call.request), the response (call.response), and helpers like call.respondText() and call.respond().
Environment and Logging
Access environment configuration inside a module via environment.config. Logging uses SLF4J; add a logback.xml to src/main/resources/ to configure log levels and output format.
Hot Reload in Development
Ktor supports hot reload during development via the development mode flag. Enable it in application.conf or programmatically: embeddedServer(Netty, port = 8080, developmentMode = true) { ... }.
Project Structure Convention
A typical Ktor project layout:
Application.kt—main()entry pointplugins/— one file per Application extension function (Routing, Serialization, Auth, etc.)routes/— route definitions grouped by domainmodels/— data classes
Quick Check
What is the purpose of an Application module function in Ktor?
Recap: Ktor Project Setup
Key takeaways:
embeddedServer(Engine, port) { ... }.start(wait=true)is the simplest entry point- Split configuration into module extension functions on
Application - Use
EngineMain+application.conffor externalized configuration - Every request handler works with
ApplicationCallfor request/response access - Group routes in a
routes/package for maintainability
Frequently asked questions
Is the “Ktor Project Setup: embeddedServer and Application Modules” lesson free?
Yes — the full text of “Ktor Project Setup: embeddedServer and Application Modules” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Ktor Project Setup: embeddedServer and Application Modules”?
Bootstrap a Ktor application, configure Netty, and define application modules. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Ktor Project Setup: embeddedServer and Application Modules” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Ktor Project Setup: embeddedServer and Application Modules
- Routing and Typed Parameters
- Content Negotiation and kotlinx.serialization
- Authentication Plugins: JWT and Session