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

anthony castillo

07/29/2020, 2:12 PM
Hello. We encountered a scenario with routes and interceptors that seems weird. Please consider the scenario below:
Copy code
fun Route.routeA() = route("/a") {
  intercept(ApplicationCallPipeline.Call) {
    <do_something_a>
  }
}

fun Route.routeB() = route("/a/{some_value}") {
  intercept(ApplicationCallPipeline.Call) {
    <do_something_b>
  }
}

routing {
  routeB()
  routeA()
}
When we send a request to
/a/some_value
, the request goes through
do_something_a
first before
do_something_b
. Is this the expected behavior? Our expectation is that only
do_something_b
would be triggered, since in this case
routeB
is not a child route of
routeA
Note: We separated the routes due to the hope that the interceptor will only be applied to the enclosing route. We have some business requirement where ACL is different on those scenarios
b

Brad Murray

07/31/2020, 12:18 AM
I don't think intercepts work in a conditional fashion like that. If you want to do something like ACL, then I'd suggest building an PermissionsFeature or similar that intercepts on the Pipeline once, and populates the call (or use the AuthContext with the info you need). Then have a helper function as a filter you can call directly inside each route (where your intercepts are now) that calculates whether to proceed or throw some permissions exception
👍 1
2 Views