Skip to content

Instantly share code, notes, and snippets.

@ayosec
Last active February 10, 2017 00:52
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ayosec/4582543 to your computer and use it in GitHub Desktop.
Save ayosec/4582543 to your computer and use it in GitHub Desktop.
Spray: example with a router based on actors

Test with

$ sbt run

And then,

$ curl localhost:8080/mars/hi
$ curl localhost:8080/jupiter/hi
scalaVersion := "2.10.0"
resolvers += "spray repo" at "http://repo.spray.io"
libraryDependencies ++= Seq(
"io.spray" % "spray-can" % "1.1-M7",
"io.spray" % "spray-routing" % "1.1-M7",
"com.typesafe.akka" %% "akka-actor" % "2.1.0"
)
package universe
import akka.actor.Actor
import spray.routing.HttpServiceActor
class Jupiter extends Actor with HttpServiceActor {
def receive = runRoute {
(get & path("hi")) { complete("Hi from Jupiter!") }
}
}
package universe
import akka.actor.Props
import spray.routing.SimpleRoutingApp
object Main extends App with SimpleRoutingApp {
lazy val mars = system.actorOf(Props[Mars])
lazy val jupiter = system.actorOf(Props[Jupiter])
startServer(interface = "localhost", port = 8080) {
pathPrefix("jupiter") { ctx => jupiter ! ctx } ~
pathPrefix("mars") { ctx => mars ! ctx }
}
}
package universe
import akka.actor.Actor
import spray.routing.HttpServiceActor
class Mars extends Actor with HttpServiceActor {
def receive = runRoute {
(get & path("hi")) { complete("Hi from Mars!") }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment