Created
May 3, 2014 22:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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