How to get request context in user directive?

I am writing a custom directive in Spray that will control the speed limit for any user request.

I will have LimitManagersomewhere where the user limits and rules for each request will be processed. The only information needed for this LimitManageris userInfoand currentRoute, that is, different limits for different routes.

So I probably end up with something like this for the directive:

def ensureLimit(): Directive0 =
  if (LimitManager.isAuthorized(userInfo, currentRoute)) {
    pass
  } else {
    reject
  }

How can I get the request context inside the directive so that I can provide the correct information for mine LimitManager?

+4
source share
1 answer

Spray Route, RequestContext => Unit. Scala , DSL , , :

val route: Route = get { (ctx: RequestContext) => // this can be omitted, just for info
  ctx.complete("Hello")
}

, :

val route: Route = get { complete("Hello") }

.

! :

val route = get { ctx =>
  complete("Alloha")
}

complete ctx => ctx.complete("Hello"), .

, , , . extract, , map flatMap, :

val myDirective = extract(identity) map { ctx => /* Your directive */ } 
+7

Source: https://habr.com/ru/post/1547221/


All Articles