Adding paths to MyService

An excellent sample of directive-heavy route has been given as an answer to How to unmarshal POST params and JSON body in a single route?:

path("ElevationService" / DoubleNumber / DoubleNumber) { (long, lat) =>
  post {
    entity(as[ScriveRequest]) { scrive =>
      // use scrive, long and lat appropriately
      complete
    }
  } 
}

It combines path, /, DoubleNumber, post, entity, as, and complete in a single route.

You've already met path, get, and complete directives, so we're left off exploring /, DoubleNumber, post, and the key directive of the route entity.

FIXME Expand on the directives.

In a comment on StackOverflow Johannes Rudolph wrote:

The expression name(arguments) is usually a simple method invocation that returns a Directive. A Directive is something that when applied with a function argument will return a Route. A Route is something that can handle a request. A Directive is therefore a factory for Routes. Now, depending on the functionality (and the type) of the Directive it may "extract" values from the request. These are then provided to the inner route as extractions as shown in the general pattern you showed above.

And then the accepted answer goes on what a directive is in Spray:

A Directive accepts a function as an argument

...

Remember a Route is really just a RequestContext => Unit, so this returns a Route that, when passed a RequestContext:

  • Runs f on it, to extract the things that need extracting (e.g. URL path components)
  • Runs inner on that; inner is a function from e.g. path components to the inner route.
  • Runs that inner route, on the context.

In another answer, there's an additional explanation of what Directive is:

In Spray, a directive is a function over a Route, which is an alias for a function RequestContext => Unit. With the help of mighty Scala implicits, Routing DSL helps to hide this, and you can write stuff like this:

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

It's the same as:

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

results matching ""

    No results matching ""