Is there a reason that `createApplicationPlugin` /...
# ktor
a
Is there a reason that
createApplicationPlugin
/
createRouteScopedPlugin
are typically instantiated as values with a configuration block? In a lot of cases it is much easier to just do:
Copy code
fun MyCustomPlugin(val myDeps: MyDeps) = createRouteScopedPlugin("MyCustomPlugin") {
     ...use myDeps
}
instead of a configuration class with mutable variables.
2
s
I think it's a good approach for your scenario where
MyDeps
is a required parameter, so you avoid potentially forgetting `install(MyCustomPlugin) { deps = myDeps }`which is great! Most plugins in Ktor itself are designed to work without any required parameters and therefor they have no need for that restriction. Two comparisons: For example, falling back to env "OPENAI_TOKEN"
Copy code
val OpenAi = createApplicationPlugin("OpenAi", { OpenAiConfiguratin.Builder() }) {
  if(builder.apiKey == null) builder.apiKey(getenv("OPENAI_TOKEN"))
  val config = pluginConfig.build()

}
Instead of:
Copy code
fun OpenAi(token: String) = createApplicationPlugin("OpenAi", { OpenAiConfiguratin.Builder() }) {
  val config = pluginConfig.apiKey(token).build()
}
Remember that if you want to be able to install 2 plugins you need to implement a custom plugin with different
AttributeKey
.