bashan
bashan

Reputation: 3602

Spray routing doesn't work

Can anyone explain why this example only reacts to: localhost:8080?

package com.example

import akka.actor.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 Actor with 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 = runRoute(myRoute)
}


// this trait defines our service behavior independently from the service actor
trait MyService extends HttpService {

  val myRoute =
    path("test1") {
      get {
        respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
          complete {
            <html>
              <body>
                <h1>test1</h1>
              </body>
            </html>
          }
        }
      }
    } ~
    path("test2") {
      get {
        respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
          complete {
            <html>
              <body>
                <h1>TEST SPRAY</h1>
              </body>
            </html>
          }
        }
      }
    }
}

Upvotes: 1

Views: 186

Answers (1)

Eugene Zhulenev
Eugene Zhulenev

Reputation: 9734

I don't know how you start your server, but this code gives me exactly what you expect test1 for localhost:8080/test1 and TEST SPRAY for test2

object MyServiceServer extends App {

  implicit val system = ActorSystem("my-service")

  val service = system.actorOf(Props[MyServiceActor], "my-service")


  IO(Http) ! Http.Bind(service, "localhost", 8080)
}

Upvotes: 2

Related Questions