任何人都可以提供一些关于如何在喷雾中构建路由的好方法吗? 我的路线编辑包含路由文件....喷射REST路由 - 过于冗长
pathPrefix("customers") {
pathEnd {
get {
handleRejections(RejectionHandler.apply(handleMissingAuthSessionKey orElse handleValidationErrorAsUnauthorized)) {
withSessionKey[String]("groups") { g =>
validate(g.contains("admin"), "Not authorized") {
complete {
m.getCustomers
}
}
}
}
}
} ~
path(IntNumber) { id =>
post {
handleRejections(RejectionHandler.apply(handleMissingAuthSessionKey orElse handleValidationErrorAsUnauthorized)) {
withSessionKey[String]("groups") { g =>
validate(g.contains("admin"), "Not authorized") {
entity(as[Customer]) { c =>
complete {
m.updateCustomer(c).map {
case 0 => StatusCodes.UnprocessableEntity
case 1 => StatusCodes.Accepted
case _ => StatusCodes.InternalServerError
}.handleSuccessWith { case _ =>
siblingWorkers ! Push("customers", None)
}
}
}
}
}
}
} ~
delete {
withSessionKey[String]("groups") { g =>
validate(g.contains("admin"), "Not authorized") {
complete {
m.deleteCustomer(id).map {
case 0 => StatusCodes.UnprocessableEntity
case 1 => StatusCodes.Accepted
case _ => StatusCodes.InternalServerError
}.handleSuccessWith { case _ =>
siblingWorkers ! Push("customers", None)
}
}
}
}
}
} ~
path("new") {
handleRejections(RejectionHandler.apply(handleMissingAuthSessionKey orElse handleValidationErrorAsUnauthorized)) {
post {
withSessionKey[String]("groups") { g =>
validate(g.contains("admin"), "Not authorized") {
entity(as[Customer]) { c =>
complete {
m.insertCustomer(c).handleSuccessWith { case _ =>
siblingWorkers ! Push("customers", None)
}
}
}
}
}
}
}
}
} ~
pathPrefix("groups") {
pathEnd {
get {
handleRejections(RejectionHandler.apply(handleMissingAuthSessionKey orElse handleValidationErrorAsUnauthorized)) {
withSessionKey[String]("groups") { g =>
validate(g.contains("admin"), "Not authorized") {
complete {
m.getGroups
}
}
}
}
}
} ~
pathPrefix(IntNumber) { groupId =>
pathEnd {
complete {
m.getGroupById(groupId)
}
} ~
path("users") {
complete {
m.getGroupById(groupId).flatMap { groupO =>
groupO.map { group =>
m.getUsers(group)
}.getOrElse(Future.successful(Seq()))
}
}
}
} ~
pathPrefix(Segment) { groupName =>
pathEnd {
complete {
m.getGroupByName(groupName)
}
} ~
path("users") {
complete {
m.getGroupByName(groupName).flatMap { groupO =>
groupO.map { group =>
m.getUsers(group)
}.getOrElse(Future.successful(Seq()))
}
}
}
}
} ~
pathPrefix("users") {
path("me") {
handleRejections(RejectionHandler.apply(handleMissingAuthSessionKey orElse handleValidationErrorAsUnauthorized)) {
withSessionKey[String]("userId") { uid =>
complete {
m.getUserById(uid.toInt).map(_.get)
}
}
}
} ~
path("new") {
handleRejections(RejectionHandler.apply(handleMissingAuthSessionKey orElse handleValidationErrorAsUnauthorized orElse RejectionHandler.Default)) {
withSessionKey[String]("groups") { g =>
validate(g.contains("admin"), "Not authorized") {
entity(as[NewUser]) { r =>
complete {
m.addUser(r).handleCompletionWith{ _ => siblingWorkers ! Push("users", None)}
}
}
}
}
}
} ~
pathPrefix(IntNumber) { uid =>
pathEnd {
get {
handleRejections(RejectionHandler.apply(handleMissingAuthSessionKey orElse handleValidationErrorAsUnauthorized orElse RejectionHandler.Default)) {
withSessionKey[String]("groups") { g =>
validate(g.contains("admin"), "Not authorized") {
complete {
m.getUserById(uid)
}
}
}
}
} ~
post {
handleRejections(RejectionHandler.apply(handleMissingAuthSessionKey orElse handleValidationErrorAsUnauthorized orElse RejectionHandler.Default)) {
withSessionKey[String]("groups") { g =>
validate(g.contains("admin"), "Not authorized") {
entity(as[User]) { u =>
complete {
m.updateUser(u).map {
case 0 => StatusCodes.UnprocessableEntity
case 1 => StatusCodes.Accepted
case _ => StatusCodes.InternalServerError
}.handleCompletionWith { _ => siblingWorkers ! Push("users", None) }
}
}
}
}
}
} ~
delete {
handleRejections(RejectionHandler.apply(handleMissingAuthSessionKey orElse handleValidationErrorAsUnauthorized orElse RejectionHandler.Default)) {
withSessionKey[String]("groups") { g =>
validate(g.contains("admin"), "Not authorized") {
complete {
m.deleteUserById(uid).map {
case 0 => StatusCodes.UnprocessableEntity
case 1 => StatusCodes.Accepted
case _ => StatusCodes.InternalServerError
}.handleCompletionWith{ _ => siblingWorkers ! Push("groups", None)}
}
}
}
}
}
} ~
pathPrefix("groups") {
path("new") {
entity(as[NewUserGroup]) { g =>
complete {
m.addUserGroup(UserGroup(uid, g.id)).map {
case 0 => StatusCodes.UnprocessableEntity
case 1 => StatusCodes.Accepted
case _ => StatusCodes.InternalServerError
}.handleCompletionWith{ _ => siblingWorkers ! Push("groups", None)}
}
}
} ~
pathEnd {
get {
complete {
m.getUserGroups(uid)
}
} ~
post {
entity(as[Seq[Int]]) { groups =>
complete {
m.setUserGroups(uid, groups).map {
case Some(x) if x < groups.length => StatusCodes.UnprocessableEntity
case Some(x) if x == groups.length => StatusCodes.OK
case _ => StatusCodes.InternalServerError
}.handleCompletionWith{ _ => siblingWorkers ! Push("users", None)}
}
}
}
}
}
} ~
pathEnd {
get {
handleRejections(RejectionHandler.apply(handleMissingAuthSessionKey orElse handleValidationErrorAsUnauthorized)) {
withSessionKey[String]("groups") { g =>
validate(g.contains("admin"), "Not authorized") {
complete {
m.getUsers
}
}
}
}
}
}
将如何与已提取的参数(上面的隐式会话)这项工作? –
您可以在每条路由的构造函数中声明隐式参数 – Nyavro
这是否意味着每次请求都会重新评估路由?与在启动时进行评估相反,只是在每次请求时都运行? –