Product Engineer, CTO & a Beer Enthusiast
Experiments, thoughts and scripts documented for posterity.
// we don't implement our route structure directly in the service actor because
// we want to be able to test it independently, without having to spin up an actor
class CustomerServiceActor extends Actor with CustomerService {
// the HttpService trait defines only one abstract member, which
// connects the services environment to the enclosing actor or test
def actorRefFactory = context
// this actor only runs our route, but you could add
// other things here, like timeout handling
def receive = runRoute(customerRoutes)
}
// this trait defines our service behavior independently from the service actor
trait CustomerService extends HttpService {
val customerRoutes= ??? //routes are defined/implemented here
trait CustomerService extends HttpService {
val customerRoutes =
path("addCustomer") {
post {
complete {
//insert customer information into a DB
"Success"
}
}
} ~
path("getCustomer" / Segment) { customerId =>
get {
complete {
//get customer from db using customerId as Key
s"success ${customerId}"
}
}
}
}
The urls for accessing the above routes are as :
[GET] http://localhost:8080/getCustomer/123
[POST] http://localhost:8080/addCustomer
class CustomerServiceActor extends Actor with CustomerService with AjaxService {
def actorRefFactory = context
def receive = runRoute(customerRoutes ~ ajaxRoutes)
}
trait AjaxService extends HttpService {
val ajaxRoutes =
path("search" / Segment) { query =>
get {
complete {
//Free text search implementation
s"success ${query}"
}
}
}
}
// this trait defines our service behavior independently from the service actor
trait CustomerService extends HttpService {
//. . . . . . //existing code
}
The above project can be downloaded at https://github.com/karthik20522/SprayLearning