kotlin-ktor
Ktor framework - routing, authentication, WebSockets
When & Why to Use This Skill
The Kotlin Ktor skill is a specialized developer tool designed to accelerate the creation of high-performance, asynchronous backends. It provides expert assistance in implementing core Ktor features including RESTful routing, secure JWT authentication, and real-time WebSockets, ensuring your Kotlin applications are production-ready, idiomatic, and scalable.
Use Cases
- Rapidly scaffolding RESTful API endpoints with structured nested routing and dynamic parameter validation.
- Implementing robust security layers using JWT (JSON Web Tokens) for secure user authentication and authorization.
- Developing real-time, bi-directional communication features such as live chat or notification systems via WebSockets.
- Streamlining the development lifecycle by generating comprehensive integration tests and troubleshooting common framework configuration issues.
| name | kotlin-ktor |
|---|---|
| description | Ktor framework - routing, authentication, WebSockets |
| version | "1.0.0" |
| sasmp_version | "1.3.0" |
| bonded_agent | 05-kotlin-backend |
| bond_type | PRIMARY_BOND |
| timeout_ms | 30000 |
| max_attempts | 3 |
| backoff | exponential |
| initial_delay_ms | 1000 |
| - name | ktor_version |
| type | string |
| validation | "^(routing|plugins|auth|websocket|testing)$" |
| default | "2.3.8" |
| level | info |
| events | [skill_invoked, topic_loaded, error_occurred] |
Kotlin Ktor Skill
Build production-ready backends with Ktor.
Topics Covered
Routing
fun Application.module() {
install(ContentNegotiation) { json() }
routing {
route("/api/v1") {
get("/users") { call.respond(userService.findAll()) }
get("/users/{id}") {
val id = call.parameters["id"]?.toLongOrNull()
?: throw BadRequestException("Invalid ID")
call.respond(userService.findById(id) ?: throw NotFoundException())
}
}
}
}
JWT Authentication
install(Authentication) {
jwt("auth") {
verifier(JWT.require(Algorithm.HMAC256(secret)).build())
validate { credential ->
if (credential.payload.getClaim("userId").asString().isNotEmpty())
UserPrincipal(credential.payload)
else null
}
}
}
authenticate("auth") { userRoutes() }
Testing
@Test
fun `GET users returns list`() = testApplication {
application { module() }
client.get("/api/v1/users").apply {
assertThat(status).isEqualTo(HttpStatusCode.OK)
}
}
Troubleshooting
| Issue | Resolution |
|---|---|
| 404 for valid route | Order specific routes before wildcards |
| JSON not parsed | Install ContentNegotiation plugin |
Usage
Skill("kotlin-ktor")