Hi. In Ktor routing DSL, the receiver of `get` (an...
# ktor
d
Hi. In Ktor routing DSL, the receiver of
get
(and others) have signature of
PipelineContext<Unit, ApplicationCall>
. Where is the first type (
Unit
) coming from and is this something I can change by dsl function to be for example some API context data class that is common for a group of routes?
a
What do you want to achieve by passing a different type?
d
I would like to use it to store some context common to all the API endpoints under it.
Copy code
route("/api/v1"){
    // parse important data common to all api requests and save them as subject
    route("/endpoint1") {
        head { 
            val ctx = subject
            //do sth with ctx
        }
        get {
            val ctx = subject
            //do sth with ctx
        }
    }
    get("/endpoint2") {
        val ctx = subject
        //do sth with ctx
    }
}
a
Why can’t you just use a shared variable?
d
I am not sure I understand what you mean by that. Can you give me example how would I achieve this?
a
I mean something like this:
Copy code
routing {
    val ctx = Object()
    route("/endpoint1") {
        head {
            //do sth with ctx
        }
        get {
            //do sth with ctx
        }
    }
    get("/endpoint2") {
        //do sth with ctx
    }
}
d
@Aleksei Tirman [JB] but I would like to access headers and other request-specific params in the context construction (
Object
in your snippet), which is not possible since the receiver of the
route
function doesn't have access to those (afaik)