I’m learning Kotlin, Ktor 1 (server) and Koin. How...
# koin
d
I’m learning Kotlin, Ktor 1 (server) and Koin. How could I inject the app context (ApplicationCall) and make it available everywhere ? For instance, instead of passing the context as param here, I’d like to be able to have access to it from within my
hasSignedUrlMiddleware
:
Copy code
get("/demo/signed-url") {
            hasSignedUrlMiddleware(context as ApplicationCall)
            call.respondText("This is a text response from /demo/signed-url")
        }
Copy code
fun Application.hasSignedUrlMiddleware(context: ApplicationCall) {
    routing {
        val signature = context.request.queryParameters["signature"]
        if(signature != "1234") {
            throw AuthorizationException("Sorry you are not authorized")
        }
    }
}
I tried injecting it but it doesn’t work :
Copy code
interface CtxService {
    fun getCtx(): ApplicationCall
}

class CtxServiceImpl(val context: ApplicationCall) : CtxService {
    override fun getCtx() = context
}

val ctxAppModule = module {
    single<CtxService> { CtxServiceImpl(get()) }
}
I get this error:
500: org.koin.core.error.InstanceCreationException: Could not create instance for [Singleton:'app.plugins.CtxService']