Is it possible to install a feature dynamically? I...
# ktor
j
Is it possible to install a feature dynamically? I’m looking into integrating a login feature through through an OAuth provider stating in its doc that endpoints should be fetch and not stored statically since they could (rarely) change over time. From what I see in the doc from Ktor, the OAuth configuration is done directly into the Application block and it’s static
a
If you have a reference to an
Application
instance then you can install any feature there "dynamically". Here is an example:
Copy code
val feature = object : ApplicationFeature<ApplicationCallPipeline, Unit, Unit> {
    override val key: AttributeKey<Unit> = AttributeKey("MyFeature")

    override fun install(pipeline: ApplicationCallPipeline, configure: Unit.() -> Unit) {
        pipeline.sendPipeline.intercept(ApplicationSendPipeline.Before) {
            println("Requested URL is ${call.request.uri}")
        }
    }
}

fun main() {
    val server = embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
        routing {
            get("/") {
                call.respondText { "root" }
            }
        }
    }
    server.start()
    server.application.install(feature)
}
j
alright, I was not sure if it was possible to do something like that. Thanks 🙂