https://kotlinlang.org logo
Title
z

zero_coding

08/22/2020, 9:54 AM
What is the best practice to abstract out the body handler?
j

John O'Reilly

08/22/2020, 9:59 AM
One approach is to create 
Route
 extension function....something like
fun Route.apiRoute() {

    route("/api") {
        get("/interests") {
            call.respondText("hello world")
        }
    }
}
then you'd have
routing {
    apiRoute()
}
👍 2
z

zero_coding

08/22/2020, 10:13 AM
aha..wow
Man...and that is possible?
so elegant.....
thanks a lot @John O'Reilly
e

Eric Grimsborn

08/24/2020, 12:00 PM
A bit late but I usually do this in my projects: Types.kt
typealias RouteHandler = suspend PipelineContext<Unit, ApplicationCall>.(Unit) -> Unit
Handlers.kt
val helloWorld: RouteHandler = {
    call.respondText("hello world")
}
Routes.kt
routing {
    route("/api") {
        get("/interests", helloWorld)
    }
}