https://kotlinlang.org logo
#ktor
Title
# ktor
h

hfhbd

04/11/2022, 4:01 AM
Ktor 2.0: Should you apply @KtorDsl to a custom plugin? To the variable
@KtorDsl val myPlugin = createRouteScopedPlugin("myPlugin, ::Config) {}
AND to
@KtorDsl class Config
too?
If I apply the annotation, the implicit parameter does not work, and I need to add
this@routing.install
and
this@routing.get
...
a

Aleksei Tirman [JB]

04/11/2022, 11:54 AM
@Rustam Siniukov
r

Rustam Siniukov

04/11/2022, 12:43 PM
can you post a code snippet with example, please?
Copy code
@KtorDsl
val RateLimit = createRouteScopedPlugin("RateLimit, ::Config) {}

@KtorDsl
class Config

routing {
  get {
    call.respondText { "42" }
  }
  route("/login") {
    this@routing.install(RateLimit) {
      this@route.get {
        call.respondText { "/login called" }
      }
    }
  }
}
r

Rustam Siniukov

04/11/2022, 5:19 PM
but
KtorDsl
has
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPEALIAS, AnnotationTarget.TYPE, AnnotationTarget.FUNCTION)
you can’t apply it to
val RateLimit = createRouteScopedPlugin("RateLimit, ::Config) {}
h

hfhbd

04/11/2022, 6:57 PM
Oh, I simplified the sample too much. In my plugin, I used a function:
@KtorDsl fun RateLimit(requiredParameter) = createRouteScopedPlugin(...)
. But in the end,
install
works without explicit
this
. But
get
does not. At least the docs are wrong.
3 Views