Routes define the rules for dispatching requests to its intended Akka actor processors. It is implemented as a
list of PartialFunctions.
To assist with routing, use the following extractors:
HTTP Method: GET, POST, PUT, DELETE, HEAD, CONNECT, OPTIONS, TRACE
HTTP Path: Path, PathSegments, PathRegex
HTTP Host: Host, HostSegments, HostRegex
HTTP Query String: QueryString, QueryStringRegex
Example of a single list of partial functions:
val r = Routes({
case GET(PathSegments("record" :: id :: Nil)) => {
...
}
case PathSegments("record" :: id :: Nil) => {
...
}
})
Example of 2 lists of partial functions:
val r = Routes(
{
case GET(PathSegments("record" :: id :: Nil)) => {
...
}
case PathSegments("record" :: id :: Nil) => {
...
}
},
{
case PUT(Host("aaa.abc.com")) & Path("/test1") => {
...
}
case Host("aaa.abc.com") & Path("/test2") => {
...
}
})
This area of code uses extractors or, the unapply method. It is important to understand
how to use these patterns.
ctx @ is scala's variable binding pattern. It will be assigned the value of the matching ProcessingContext
GET(x) and Path(y) are extractors. In the case of GET, x is the same ProcessingContext as ctx. For Path,
y is the path as a string. In other words, the value in between the parentheses is the return value of the
associated unapply method.
- & chains together extractors like a logical AND
This [[http://daily-scala.blogspot.com.au/2010/02/chaining-partial-functions-with-orelse.html article]]
explains how chaining of PartialFunction works using orElse.
Routes define the rules for dispatching requests to its intended Akka actor processors. It is implemented as a list of PartialFunctions.
To assist with routing, use the following extractors:
Example of a single list of partial functions:
val r = Routes({ case GET(PathSegments("record" :: id :: Nil)) => { ... } case PathSegments("record" :: id :: Nil) => { ... } })Example of 2 lists of partial functions:
val r = Routes( { case GET(PathSegments("record" :: id :: Nil)) => { ... } case PathSegments("record" :: id :: Nil) => { ... } }, { case PUT(Host("aaa.abc.com")) & Path("/test1") => { ... } case Host("aaa.abc.com") & Path("/test2") => { ... } })This area of code uses extractors or, the
unapplymethod. It is important to understand how to use these patterns.ctx @is scala's variable binding pattern. It will be assigned the value of the matching ProcessingContextGET(x)andPath(y)are extractors. In the case ofGET,xis the same ProcessingContext asctx. ForPath,yis the path as a string. In other words, the value in between the parentheses is the return value of the associatedunapplymethod. -&chains together extractors like a logical AND This [[http://daily-scala.blogspot.com.au/2010/02/chaining-partial-functions-with-orelse.html article]] explains how chaining ofPartialFunctionworks usingorElse.