Im contemplating using koin with ktor on my backen...
# koin
z
Im contemplating using koin with ktor on my backend after falling in love with it on my frontend a while back. My use-case that brought it up is that Id like to be able to specify
[X-Debug] = true
for calls from my client, and have my backend inject classes with varying configurations based on it (different api keys). Is my approach sane or do you have any other recommendations? • Create two scopes that will outlive any route call (I dont want them recreated all the time) • Check the call header to find the value, then grab the appropiate scope and inject stuff using it • Preferrably, I can write an extension function that just does all of this so that I can just write
by inject<SomeService>()
inside
PipelineContext<Unit, ApplicationCall>
It seems that qualifier does exactly what I want 🙂
In case it helps anyone else out there:
Copy code
context(Route)
inline fun <reified T : Any> PipelineContext<*, ApplicationCall>.inject(): Lazy<T> {
    val qualifier =
        call.request.header(DebugAttribute)?.let { attribute ->
            qualifier(attribute)
        }
    
    return getKoin().inject<T>(qualifier)
}