Routes define the rules for dispatching events to its intended Akka handlers. It is implemented as a
list of PartialFunctions.
To assist with routing, Socko has 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, also known as the unapply method. It is important to understand
how to use these patterns.
event @ is scala's variable binding pattern. It will be assigned the value of the matching SockoEvent
GET(x) and Path(y) are extractors. In the case of GET, x is the same SockoEvent as event. 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 article
explains how chaining of PartialFunction works using orElse.
Routes define the rules for dispatching events to its intended Akka handlers. It is implemented as a list of PartialFunctions.
To assist with routing, Socko has the following extractors:
Example of a single list of partial functions:
Example of 2 lists of partial functions:
This area of code uses extractors, also known as the
unapplymethod. It is important to understand how to use these patterns.event @is scala's variable binding pattern. It will be assigned the value of the matching SockoEventGET(x)andPath(y)are extractors. In the case ofGET,xis the same SockoEvent asevent. 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 logicalANDThis article explains how chaining of
PartialFunctionworks usingorElse.