[关闭]
@MiloXia 2015-08-07T15:36:10.000000Z 字数 2685 阅读 1747

App[后端开发] Wiki

开发


部署测试流程

  1. $ ssh qiaobutang@10.1.1.223
  2. $ cd apptest
  3. $ jps -lvm | grep 8888
  4. $ kill <pid>
  5. $ ./start_resume.sh

Restful API规范

API 命名规则

URL Pattern Matching

Notice:

  1. case req @ Req("group" :: ObjectIdExtractor(groupId) :: "post" :: Nil, "json", PostRequest)
  1. subject <- req.param("subject").failMsg("请输入主题")

如下方式会存在问题

  1. case req @ Req("jobs" :: suffix :: Nil, "json", GetRequest) if suffix == "recommend"
  2. //warning 如果不加守卫,可能会把别的API(如: jobs/apply/.. )给匹配到
  1. val Suffix = "recommend"
  2. case req @ Req("jobs" :: Suffix :: Nil, "json", GetRequest)

返回值结构

基本结构

一个api 返回的基本json结构

  1. {
  2. "resultCode": status code
  3. "resultBody": {
  4. ...
  5. }
  6. ....
  7. }

返回值类型

每个API的返回值类型为Box[JObject]

  1. type DispatchPF = PartialFunction[Req, Box[JObject]]

返回方式

  1. //1. Full
  2. Full(error ~ ("failureCauses" -> fieldError.map(_.msg.toString())))
  3. //2. Box.legacyNullTest
  4. Box.legacyNullTest {
  5. "categories" -> BBSGroup.Categories.values.toList.sortBy(_.id).map(categoryInfo)
  6. }
  7. //3. Box.map
  8. getAccountId_!(req).map { accountId =>
  9. ("json" -> "ok")
  10. }

status code

forbidden 403

Notice: 目前还未有需要主动返回403的情况

error 400

触发原因:

基本结构:

  1. {
  2. "resultCode": 400
  3. "failureCause" : "...."
  4. }

Notice: 建议调用errorWith(msg)方法取代直接返回 error ~ ("failureCause" -> msg)

NotFound 404

对于一些resource/{id}的请求,在查找不到(包含逻辑删除)该id的对象时返回notFound

  1. case req @ Req("group" :: "post" :: ObjectIdExtractor(postId) :: "like" :: Nil, "json", PostRequest) =>
  2. getAccountId_!(req).map { accountId =>
  3. BBSPost.getLiveById(postId).map[JObject] { post =>
  4. ...
  5. }.getOrElse(notFound)
  6. }

自定义code

自定义: 和client端商量后选取一个code, 作为某个api的返回值
作用域: 仅限于单个api中

  1. complete(406) //告诉client端,重复操作了

Notice: 加注释

NoMessage

对于Post等不需要返回数据的请求, 返回NoMessage

  1. case req @ Req("group" :: ObjectIdExtractor(groupId) :: "join" :: Nil, "json", PostRequest) =>
  2. getAccountId_!(req).map { accountId =>
  3. ...
  4. NoMessage
  5. }

Error Message

failureCause 单个错误提示

  1. errorWith(msg)
  2. error ~ ("failureCause" -> msg)

failureCauses 多个错误提示

  1. error ~ ("failureCauses" -> fieldError.map(_.msg.toString()))

代码结构

包结构

  1. com.qiaobutang.resume.app
  2. |_App.scala (主入口)
  3. |_api (存放resource)
  4. |_Group.scala
  5. |_Job.scala
  6. |_Career.scala
  7. |_....scala

Getting Started

  1. object Resource extends AppResource {
  2. ...
  3. }
  1. val serve: DispatchPF = {
  2. ...
  3. }
  1. val serve: DispatchPF = {
  2. case Req("path" :: Nil, "json", GetRequest) => {
  3. ...
  4. }
  5. ...
  1. override val services: LiftRules.DispatchPF =
  2. List(..., Resource).map { r => ...

app package object

RangeQuery

3n+1规则

Getting Started

查询优化

其它通用组件

Default Resolution

获取默认分辨率

  1. defaultResolution(req: Req)

SocialProfile

获取用户头像,性别,昵称等社交信息

  1. Scene.getSocialProfile(resolution: String)(accountId: ObjectId)

图片上传

  1. Group.uploadImageAndGetIds(req: Req, imageModel: MongoModelMeta[T], accountId: ObjectId)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注