Spray.ioルートを複数のファイルに分割するための確かな例や構造は見つかりませんでした。ルートの現在の構造が非常に煩雑になることがわかりました。非常に単純なRESTAPIアプリの場合は、ルートをさまざまな「コントローラー」に抽象化すると便利です。
ドキュメントはあまり役に立たないようです: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))
}
}
}
}
これについて助けてくれてありがとう!