Skip to content

Instantly share code, notes, and snippets.

@ayosec ayosec/0README.md

Last active Feb 10, 2017
Embed
What would you like to do?
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
You can’t perform that action at this time.