Can Spray.io routes be split into multiple “Controllers”?

前端 未结 3 754
-上瘾入骨i
-上瘾入骨i 2020-12-23 17:56

I haven\'t found a solid example or structure to splitting up Spray.io routes into multiple files. I am finding that the current structure of my routes are going to become v

3条回答
  •  有刺的猬
    2020-12-23 18:18

    You can combine routes from different "Controllers" using ~ combinator.

    class AccountServiceActor extends Actor with HttpService {
    
      def actorRefFactory = context
    
      def receive = handleTimeouts orElse runRoute(
      new AccountService1.accountService1 ~  new AccountService2.accountService2)
    
      def handleTimeouts: Receive = {
        case Timeout(x: HttpRequest) =>
          sender ! HttpResponse(StatusCodes.InternalServerError, "Request timed out.")
      }
    }
    
    
    
    class AccountService1 extends HttpService {
    
      val accountService1 = {
        get {
          path("") {
            respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
              complete(index)
            }
          }
        }
    }
    
    
    class AccountService2 extends HttpService {
    
      val accountService2 = {
        get {
          path("someotherpath") {
            respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
              complete(index)
            }
          }
        }
    }
    

提交回复
热议问题