add docs about how to write api application
Showing
3 changed files
with
107 additions
and
3 deletions
docs/zh/API.md
0 → 100644
| 1 | # API应用开发入门 | ||
| 2 | Go是非常适合用来开发API应用的,而且我认为也是Go相对于其他动态语言的最大优势应用。beego在开发API应用方面提供了非常强大和快速的工具,方便用户快速的建立API应用原型,专心业务逻辑就行了。 | ||
| 3 | |||
| 4 | |||
| 5 | ## 快速建立原型 | ||
| 6 | bee快速开发工具提供了一个API应用建立的工具,在gopath/src下的任意目录执行如下命令就可以快速的建立一个API应用: | ||
| 7 | |||
| 8 | `bee api beeapi` | ||
| 9 | |||
| 10 | ## 应用的目录结构 | ||
| 11 | 应用的目录结构如下所示: | ||
| 12 | |||
| 13 | ``` | ||
| 14 | ├── conf | ||
| 15 | │ └── app.conf | ||
| 16 | ├── controllers | ||
| 17 | │ └── default.go | ||
| 18 | ├── models | ||
| 19 | │ └── object.go | ||
| 20 | └── main.go | ||
| 21 | ``` | ||
| 22 | |||
| 23 | ## 源码解析 | ||
| 24 | |||
| 25 | - app.conf里面主要针对API的配置如下: | ||
| 26 | |||
| 27 | autorender = false //API应用不需要模板渲染,所以关闭自动渲染 | ||
| 28 | |||
| 29 | copyrequestbody = true //RESTFul应用发送信息的时候是raw body,而不是普通的form表单,所以需要额外的读取body信息 | ||
| 30 | |||
| 31 | - main.go文件主要针对RESTFul的路由注册 | ||
| 32 | |||
| 33 | `beego.RESTRouter("/object", &controllers.ObejctController{})` | ||
| 34 | |||
| 35 | 这个路由可以匹配如下的规则 | ||
| 36 | |||
| 37 | <table> | ||
| 38 | <tr> | ||
| 39 | <th>URL</th> <th>HTTP Verb</th> <th>Functionality</th> | ||
| 40 | </tr> | ||
| 41 | <tr> | ||
| 42 | <td>/object</td> <td>POST</td> <td>Creating Objects</td> | ||
| 43 | </tr> | ||
| 44 | <tr> | ||
| 45 | <td>/object/objectId</td> <td>GET</td> <td>Retrieving Objects</td> | ||
| 46 | </tr> | ||
| 47 | <tr> | ||
| 48 | <td>/object/objectId</td> <td>PUT</td> <td>Updating Objects</td> | ||
| 49 | </tr> | ||
| 50 | <tr> | ||
| 51 | <td>/object</td> <td>GET</td> <td>Queries</td> | ||
| 52 | </tr> | ||
| 53 | <tr> | ||
| 54 | <td>/object/objectId</td> <td>DELETE</td> <td>Deleting Objects</td> | ||
| 55 | </tr> | ||
| 56 | </table> | ||
| 57 | |||
| 58 | - ObejctController实现了对应的方法: | ||
| 59 | |||
| 60 | ``` | ||
| 61 | type ObejctController struct { | ||
| 62 | beego.Controller | ||
| 63 | } | ||
| 64 | |||
| 65 | func (this *ObejctController) Post(){ | ||
| 66 | |||
| 67 | } | ||
| 68 | |||
| 69 | func (this *ObejctController) Get(){ | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | func (this *ObejctController) Put(){ | ||
| 74 | |||
| 75 | } | ||
| 76 | |||
| 77 | func (this *ObejctController) Delete(){ | ||
| 78 | |||
| 79 | } | ||
| 80 | ``` | ||
| 81 | |||
| 82 | - models里面实现了对应操作对象的增删改取等操作 | ||
| 83 | |||
| 84 | ## 测试 | ||
| 85 | |||
| 86 | - 添加一个对象: | ||
| 87 | |||
| 88 | `curl -X POST -d '{"Score":1337,"PlayerName":"Sean Plott"}' http://127.0.0.1:8080/object` | ||
| 89 | |||
| 90 | 返回一个相应的objectID:astaxie1373349756660423900 | ||
| 91 | |||
| 92 | - 查询一个对象 | ||
| 93 | |||
| 94 | `curl -X GET http://127.0.0.1:8080/object/astaxie1373349756660423900` | ||
| 95 | |||
| 96 | - 查询全部的对象 | ||
| 97 | |||
| 98 | `curl -X GET http://127.0.0.1:8080/object` | ||
| 99 | |||
| 100 | - 更新一个对象 | ||
| 101 | |||
| 102 | `curl -X PUT -d '{"Score":10000}'http://127.0.0.1:8080/object/astaxie1373349756660423900` | ||
| 103 | |||
| 104 | - 删除一个对象 | ||
| 105 | |||
| 106 | `curl -X DELETE http://127.0.0.1:8080/object/astaxie1373349756660423900` |
| ... | @@ -43,6 +43,7 @@ beego是一个类似tornado的Go应用框架,采用了RESTFul的方式来实 | ... | @@ -43,6 +43,7 @@ beego是一个类似tornado的Go应用框架,采用了RESTFul的方式来实 |
| 43 | * [一步一步开发应用](Tutorial.md) | 43 | * [一步一步开发应用](Tutorial.md) |
| 44 | * [beego案例](Application.md) | 44 | * [beego案例](Application.md) |
| 45 | * [热升级](HotUpdate.md) | 45 | * [热升级](HotUpdate.md) |
| 46 | * [API应用开发入门](API.md) | ||
| 46 | 47 | ||
| 47 | 48 | ||
| 48 | # API接口 | 49 | # API接口 | ... | ... |
| ... | @@ -6,9 +6,6 @@ import ( | ... | @@ -6,9 +6,6 @@ import ( |
| 6 | "github.com/astaxie/beego/example/beeapi/models" | 6 | "github.com/astaxie/beego/example/beeapi/models" |
| 7 | ) | 7 | ) |
| 8 | 8 | ||
| 9 | type ResponseInfo struct { | ||
| 10 | } | ||
| 11 | |||
| 12 | type ObejctController struct { | 9 | type ObejctController struct { |
| 13 | beego.Controller | 10 | beego.Controller |
| 14 | } | 11 | } | ... | ... |
-
Please register or sign in to post a comment