NLinker

Scala Spray sample

Mar 27th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.15 KB | None | 0 0
  1. class MyServiceActor extends Actor with MyService {
  2.  
  3.   // the HttpService trait defines only one abstract member, which
  4.   // connects the services environment to the enclosing actor or test
  5.   def actorRefFactory = context
  6.  
  7.   // this actor only runs our route, but you could add
  8.   // other things here, like request stream processing
  9.   // or timeout handling
  10.   def receive = runRoute(myRoute)
  11. }
  12.  
  13.  
  14. // this trait defines our service behavior independently from the service actor
  15. trait MyService extends HttpService {
  16.  
  17.   val myRoute =
  18.     path("dictionaries" / Segment / "suggestions"){ dictionaryId =>
  19.       get{
  20.         parameters("ngr"){ ngr =>
  21.           complete("response")
  22.         }
  23.       }
  24.     }
  25. }
  26.  
  27. object Boot extends App {
  28.  
  29.   // we need an ActorSystem to host our application in
  30.   implicit val system = ActorSystem("on-spray-can")
  31.  
  32.   // create and start our service actor
  33.   val service = system.actorOf(Props[MyServiceActor], "demo-service")
  34.  
  35.   implicit val timeout = Timeout(5.seconds)
  36.   // start a new HTTP server on port 8080 with our service actor as the handler
  37.   IO(Http) ? Http.Bind(service, interface = "localhost", port = 8080)
  38. }
Add Comment
Please, Sign In to add comment