9

当我使用 Spray.io 开发 RESTful API 时,我应该如何构建我的应用程序?

我已经看到了有关如何拆分 Spray 应用程序的答案,但我对此并不满意,因为它似乎没有使用“每个请求一个参与者”的方法。我可以根据路径将来自根参与者的请求转发到我的应用程序中的其他参与者,并在这些参与者内部定义相关路由吗?

谢谢

4

2 回答 2

8

您当然可以根据路径或其他任何方式将请求从一个参与者转发到另一个参与者。查看我的示例项目(这是示例项目的一个分支):

https://github.com/gangstead/spray-moviedb/blob/master/src/main/scala/com/example/routes/ApiRouter.scala

来自主要参与者的相关代码,接收所有请求并将它们路由到处理每个服务的其他参与者:

  def receive = runRoute {
    compressResponseIfRequested(){
      alwaysCache(simpleCache) {
        pathPrefix("movies") { ctx => asb.moviesRoute ! ctx } ~
        pathPrefix("people") { ctx => asb.peopleRoute ! ctx }
      } ~
      pathPrefix("login") { ctx => asb.loginRoute ! ctx } ~
      pathPrefix("account") { ctx => asb.accountRoute ! ctx }
    }
  }

例如电影路线:

  def receive = runRoute {
    get {
      parameters('query, 'page ? 1).as(TitleSearchQuery) { query =>
        val titleSearchResults = ms.getTitleSearchResults(query)
        complete(titleSearchResults) 
      }~
      path(LongNumber) { movieId =>  
        val movie = ms.getMovie(movieId)
        complete(movie)
      }~
      path(LongNumber / "cast") { movieId =>
        val movieCast = ms.getMovieCast(movieId)
        complete(movieCast)      
      }~
      path(LongNumber / "trailers") { movieId =>
        val trailers = ms.getTrailers(movieId)
        complete(trailers)     
      }        
    }
  }  
于 2014-05-15T22:17:29.437 回答
0

我为创建第一个完整的 REST 项目而苦苦挣扎。我发现的示例是在 hello world 级别...我读过一些博客,很少有评论,我决定创建示例项目。它基于 scala/akka/spray/mysql

它是带有 websocket 的完整工作示例,用于通知客户端数据已更改等。您可以在https://github.com/vixxx123/scalasprayslickexample上查看

这是该项目的路由示例代码:

val personCreateHandler = actorRefFactory.actorOf(RoundRobinPool(2).props(Props[CreateActor]), s"${TableName}CreateRouter")
val personPutHandler = actorRefFactory.actorOf(RoundRobinPool(5).props(Props[UpdateActor]), s"${TableName}PutRouter")
val personGetHandler = actorRefFactory.actorOf(RoundRobinPool(20).props(Props[GetActor]), s"${TableName}GetRouter")
val personDeleteHandler = actorRefFactory.actorOf(RoundRobinPool(2).props(Props[DeleteActor]), s"${TableName}DeleteRouter")

val userRoute =
    pathPrefix("person") {
        pathEnd {
            get {
                ctx => personGetHandler ! GetMessage(ctx, None)
            } ~
            post {
                entity(as[Person]) {
                    entity =>
                        ctx => personCreateHandler ! CreateMessage(ctx, entity)
                }
            }
        } ~
        pathPrefix (IntNumber){
            entityId => {
                pathEnd {
                    get {
                        ctx => personGetHandler ! GetMessage(ctx, Some(entityId))
                    } ~ put {
                        entity(as[Person]) { entity =>
                            ctx => personPutHandler ! PutMessage(ctx, entity.copy(id = Some(entityId)))
                        }
                    } ~ delete {
                        ctx => personDeleteHandler ! DeleteMessage(ctx, entityId)
                    } ~ patch {
                        ctx => personPutHandler ! PatchMessage(ctx, entityId)
                    }
                }
            }
        }
    }

并从创建演员处理程序示例:

override def receive: Receive = {

    case CreateMessage(ctx, person) =>

      val localCtx = ctx
      connectionPool withSession {
        implicit session =>
          try {
            val resId = PersonsIdReturning += person
            val addedPerson = person.copy(id = Some(resId.asInstanceOf[Int]))
            localCtx.complete(addedPerson)
            publishAll(CreatePublishMessage(TableName, localCtx.request.uri + "/" + addedPerson.id.get, addedPerson))
            L.debug(s"Person create success")
          } catch {
            case e: Exception =>
              L.error(s"Ups cannot create person: ${e.getMessage}", e)
              localCtx.complete(e)
          }
      }
  }

仍然缺少两件重要的事情:oauth2 和通过 websocket 向特定用户/连接推送通知

于 2015-04-16T13:20:24.203 回答