Skip to content

Instantly share code, notes, and snippets.

@inexplicable
Created February 12, 2014 08:52
Show Gist options
  • Save inexplicable/8952046 to your computer and use it in GitHub Desktop.
Save inexplicable/8952046 to your computer and use it in GitHub Desktop.
package com.example
import akka.actor.{ActorRef, Actor}
import spray.routing._
import spray.http._
import MediaTypes._
// 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 MyServiceActor extends MyService {
// 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 request stream processing
// or timeout handling
def receive = contextAwareReceive(runRoute(myRoute) orElse {
case _ =>
sender !! null
})
}
// this trait defines our service behavior independently from the service actor
trait MyService extends HttpService with Actor {
val myRoute =
path("") {
get {
respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
complete {
<html>
<body>
<h1>Say hello to <i>spray-routing</i> on <i>spray-can</i>!</h1>
</body>
</html>
}
}
}
}
implicit class ActorRefWithContext(actorRef:ActorRef) {
def !! (msg:Any) = {
actorRef ! msg
}
}
type Context
def contextAwareReceive(r: Actor.Receive):Actor.Receive = {
case (ctx:Context, msg:Any) =>
r(msg)
}
}
@inexplicable
Copy link
Author

implicit class for auto conversion that gives actor ref !! method
receive method enclosed for context deconstruction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment