我還沒有找到一個可靠的示例或結構來將Spray.io路由分成多個文件。我發現我的路由的當前結構將變得非常繁瑣,並且將它們抽象爲用於非常簡單的REST API應用的不同「控制器」會很好。Spray.io路線可以拆分爲多個「控制器」嗎?
文檔似乎並沒有幫助太多: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))
}
}
}
}
感謝這個幫助!
看起來像這樣做的伎倆。我想知道我是否可以編寫某種可以自動將它們組合起來的隱式,而不是手動編寫service1〜service2〜service3。謝謝! – crockpotveggies
嗯,因爲它看起來像創建某種繼承問題,取消選擇它。 '類型參數[com.threetierlogic.AccountServ ice.AccountServiceActor]不符合方法apply的類型參數範圍 [T <:akka.actor.Actor]' – crockpotveggies
Ok'case class base(actorRefFactory:ActorRefFactory)擴展HttpService {'現在的問題是HTTP請求由於以下原因而失敗:'由於當前響應狀態爲'已完成'但是應該'未完成',因此無法將GET請求的響應(部分)作爲響應(部分)發送到'/ ' – crockpotveggies