Skip to content

Instantly share code, notes, and snippets.

@natbusa
Created May 3, 2014 22:43
Show Gist options
  • Save natbusa/f53b0473117b123534ca to your computer and use it in GitHub Desktop.
Save natbusa/f53b0473117b123534ca to your computer and use it in GitHub Desktop.
Simple example of web api with statistical RPC in R and persistance in cassandra
class AnalyticsActor extends Actor {
def actorRefFactory = context
val dataActor = actorRefFactory.actorOf(Props[NoSqlActor], "cassandra-client")
val statActor = actorRefFactory.actorOf(Props[StatActor], "statistical-engine")
def receive = {
case (a: String, c: String, ctx: RequestContext) =>
val f:Future[Result] =
for {
data <- (dataActor ? (a,c)).mapTo[Timeserie]
stat <- (statActor ? (data)).mapTo[Double]
} yield Result(stat, data)
f.onComplete{
result =>
val r = result.getOrElse(Result(0,Nil))
ctx.complete(Result("a","b",r.s, r.ts ) )
}
}
class ApiServiceActor() extends Actor with ApiService {
def actorRefFactory = context
def receive = runRoute(serviceRoute)
}
trait ApiService extends HttpService {
// Create Analytics client actor
val actor = actorRefFactory.actorOf(Props[AnalyticsActor], "analytics-actor")
//curl -vv -H "Content-Type: application/json" localhost:8888/api/v1/123/567
val serviceRoute = {
pathPrefix("api" / "v1") {
pathPrefix( Segment / Segment ) {
(aid, cid) =>
get {
ctx => actor ! (aid, cid, ctx)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment