scala - Akka HTTP 路由处理
问题描述
我有以下路线设置
val routes = protectedRoute("wallet") { user =>
concat {
path("wallet" / "deposit") {
get {
completeEitherT(walletService.deposit(user._id, "dollars", 50))
}
}
path("wallet") {
get {
completeEitherT(walletService.getForUser(user._id))
}
}
}
}
功能completeEitherT
如下
def completeEitherT[T](t: EitherT[Future, DatabaseError, T])(implicit marsh: ToResponseMarshaller[T]): Route = {
onSuccess(t.value) {
case Left(x: DatabaseErrors.NotFound) =>
logger.error(x.toString)
complete(StatusCodes.NotFound)
case Left(error) => complete(StatusCodes.InternalServerError)
case Right(value) => complete(value)
}
}
该protectedRoute
指令是:
def protectedRoute(permissions: String*): Directive1[User] = new Directive1[User] {
override def tapply(f: (Tuple1[User]) => Route): Route = {
headerValueByName("Authorization") { jwt =>
Jwt.decode(jwt, jwtSecret, List(algo)) match {
case Failure(_) =>
logger.error("Unauthorized access from jwtToken:", jwt)
complete(StatusCodes.Unauthorized)
case Success(value) =>
val user = JsonParser(value.content).convertTo[User]
if (user.roles.flatMap(_.permissions).exists(permissions.contains)) f(Tuple1(user))
else complete(StatusCodes.Forbidden)
}
}
}
}
我遇到的问题是,当通过 Postman 调用时,首先定义的任何路径都会导致 404。
The requested resource could not be found.
这不是响应,completeEitherT
因为它没有记录错误
如何在同一指令中定义两个或多个路径?
解决方案
请标记为重复,我在 Google 搜索中找不到此内容,但 SO 将其显示为相关。
本质上我~
在路径之间缺少一个
val routes = protectedRoute("wallet") { user =>
concat {
path("wallet" / "deposit") {
get {
completeEitherT(walletService.deposit(user._id, "dollars", 50))
}
} ~
path("wallet") {
get {
completeEitherT(walletService.getForUser(user._id))
}
}
}
}
推荐阅读
- delphi - 从 COM 指针到记录 / 如何执行(dotNet 的)Marshal.PtrToStructure 的 Delphi 版本
- reactjs - useFetch Can't perform a React state update on an unmounted component 警告
- reactjs - 去抖动 async/await 并更新组件状态
- asp.net-mvc - 客户端不显眼的验证未按预期工作
- javascript - 等待脚本在 VueJS 中加载?
- php - 如何在不运行 Composer 的情况下在 Google App Engine 上部署 php 应用程序?
- r - R中的字符串格式解析
- c# - 如何仅屏蔽/强制使用名字和姓氏字母的大小写
- buildbot - buildbot worker 关闭时的通知
- python - 如何在处理实时 IP 摄像机视频时使用多线程