我还没有找到将 Spray.io 路由拆分为多个文件的可靠示例或结构。我发现我的路由的当前结构将变得非常繁琐,对于一个非常简单的 REST API 应用程序,将它们抽象为不同的“控制器”会很好。
文档似乎没有太大帮助:http ://spray.io/documentation/spray-routing/key-concepts/directives/#directives
这是我到目前为止所拥有的:
class AccountServiceActor extends Actor with AccountService {
def actorRefFactory = context
def receive = handleTimeouts orElse runRoute(demoRoute)
def handleTimeouts: Receive = {
case Timeout(x: HttpRequest) =>
sender ! HttpResponse(StatusCodes.InternalServerError, "Request timed out.")
}
}
// this trait defines our service behavior independently from the service actor
trait AccountService extends HttpService {
val demoRoute = {
get {
path("") {
respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
complete(index)
}
} ~
path("ping") {
complete("PONG!")
} ~
path("timeout") { ctx =>
// we simply let the request drop to provoke a timeout
} ~
path("crash") { ctx =>
throw new RuntimeException("crash boom bang")
} ~
path("fail") {
failWith(new RuntimeException("aaaahhh"))
} ~
path("riaktestsetup") {
Test.setupTestData
complete("SETUP!")
} ~
path("riaktestfetch" / Rest) { id =>
complete(Test.read(id))
}
}
}
}
感谢您对此的帮助!