如何为Http()。bindAndHandle组合不同的路由?

时间:2015-08-21 20:49:00

标签: scala akka akka-stream akka-http

我的应用程序结构如

com.app.service/
                EmailService.scala
                MonitorStatusService.scala
Server.scala

EmailService.scala

trait EmailService extends EmailProtocols {
  implicit val materializer: Materializer
  private val ecStatusResponse = ECStatus("ok", "Welcome to Server")

  val emailRoutes =
    logRequestResult("email-service") {
      pathPrefix("ec") {
        (post & entity(as[ECFailure])) { ecFailure =>
          complete {
            Postman.send(TenantFailure(ecFailure.symbolicName) getMessage)
            "OK"
          }
        }
      } ~
        path("") {
          get {
            complete {
              ecStatusResponse
            }
          }
        }
    }
}

MonitorStatusService.scala

trait MonitorStatusService extends EmailProtocols {
  implicit val materializer: Materializer
  private val ecStatusResponse = ECStatus("ok", "Welcome to Monitor Service")

  val monitorRoutes =
    logRequestResult("monitor-service") {
      pathPrefix("monitor") {
        (post & entity(as[ECFailure])) { ecFailure =>
          complete {
            Postman.send(TenantFailure(ecFailure.symbolicName) getMessage)
            "OK"
          }
        }
      } ~
        path("") {
          get {
            complete {
              ecStatusResponse
            }
          }
        }
    }
}

Server.scala

object Server extends App
with EmailService
with MonitorStatusService {
  var config: Option[Config] = None
  if (args.length > 0) {
    config = Some(ConfigFactory.parseFile(new File(args(0) trim)))
    println(config)
  }

  val host = if (config.isDefined) config.get.getString("server.host").toString else "localhost"
  val port = if (config.isDefined) config.get.getString("server.port").toInt else 8080

  implicit val system = ActorSystem("server")
  val executor = system.dispatcher
  implicit val materializer = ActorMaterializer()
  println(s"Starting server on ($host, $port)")
  Http().bindAndHandle(emailRoutes, host, port)
}

我需要什么? 我想绑定多个路由,如

Http().bindAndHandle(emailRoutes + monitorRoutes, host, port)

但是,当我这样做时,我将错误视为

enter image description here

我该怎么办?

1 个答案:

答案 0 :(得分:6)

使用~在两个对象下组合路线的方式相同。 因此,将+更改为~

虽然没有检查过,但这是它在喷雾下工作的方式所以它应该在这里。