Hello!
I know that spray directives are decoupled from the route processing code in a way that a directive may use some Future in order to modify the RequestContext.
I have the legacy code that uses transactions delimiters stored in the ThreadLocal variable, and currently I have the some custom code that wraps the Route function into a transaction by initializing that thread local variable. It works fine, however the code looks somehow ugly. Especially when I need to combine that transaction with user authorization, that must look up the user permissions in the database in the scope of the same transaction as the user.
My question is - may I write sort of a directive that will
- supply a user object to the nested route, optionally combined with some other parameters provided by other route directives (Segment, entity etc)
- ensure that the route code will be processed by the same thread as the directive is
It looks like that I need to start a future that will also include the body of the route processing.
The strained example would be something like
(path("user" / Segment) & transaction & authorized("permission1", "permission2", "permission3") {
case (userId, effectivePermissions) ⇒
...
complete("Granted")
}
where
userId is the String matched form the HTTP path, and
effectivePermissions is the set of permissions retrieved from the database.
So the transaction will start a transaction and set the ThreadLocal container, and authorized will re-use that transaction. And the body of the route will be executed in a Future.
Thanks!