af3797e1 by astaxie

Merge pull request #93 from Unknwon/master

Sync documentation of English with Chinese version.
2 parents 38b083e1 d4743fb1
1 # Getting start with API application development
2 Go is very good for developing API applications which I think is the biggest strength compare to other dynamic languages. Beego provides powerful and quick setup tool for developing API applications, which gives you more focus on business logic.
3
4
5 ## Quick setup
6 bee can setup a API application very quick by executing commands under any `$GOPATH/src`.
7
8 `bee api beeapi`
9
10 ## Application directory structure
11
12 ```
13 ├── conf
14 │ └── app.conf
15 ├── controllers
16 │ └── default.go
17 ├── models
18 │ └── object.go
19 └── main.go
20 ```
21
22 ## Source code explanation
23
24 - app.conf has following configuration options for your API applications:
25
26 - autorender = false // Disable auto-render since API applications don't need.
27 - copyrequestbody = true // RESTFul applications sends raw body instead of form, so we need to read body specifically.
28
29 - main.go is for registering routers of RESTFul.
30
31 beego.RESTRouter("/object", &controllers.ObejctController{})
32
33 Match rules as follows:
34
35 <table>
36 <tr>
37 <th>URL</th> <th>HTTP Verb</th> <th>Functionality</th>
38 </tr>
39 <tr>
40 <td>/object</td> <td>POST</td> <td>Creating Objects</td>
41 </tr>
42 <tr>
43 <td>/object/objectId</td> <td>GET</td> <td>Retrieving Objects</td>
44 </tr>
45 <tr>
46 <td>/object/objectId</td> <td>PUT</td> <td>Updating Objects</td>
47 </tr>
48 <tr>
49 <td>/object</td> <td>GET</td> <td>Queries</td>
50 </tr>
51 <tr>
52 <td>/object/objectId</td> <td>DELETE</td> <td>Deleting Objects</td>
53 </tr>
54 </table>
55
56 - ObejctController implemented corresponding methods:
57
58 type ObejctController struct {
59 beego.Controller
60 }
61
62 func (this *ObejctController) Post(){
63
64 }
65
66 func (this *ObejctController) Get(){
67
68 }
69
70 func (this *ObejctController) Put(){
71
72 }
73
74 func (this *ObejctController) Delete(){
75
76 }
77
78 - models implemented corresponding object operation for adding, deleting, updating and getting.
79
80 ## Test
81
82 - Add a new object:
83
84 curl -X POST -d '{"Score":1337,"PlayerName":"Sean Plott"}' http://127.0.0.1:8080/object
85
86 Returns a corresponding objectID:astaxie1373349756660423900
87
88 - Query a object:
89
90 `curl -X GET http://127.0.0.1:8080/object/astaxie1373349756660423900`
91
92 - Query all objects:
93
94 `curl -X GET http://127.0.0.1:8080/object`
95
96 - Update a object:
97
98 `curl -X PUT -d '{"Score":10000}'http://127.0.0.1:8080/object/astaxie1373349756660423900`
99
100 - Delete a object:
101
102 `curl -X DELETE http://127.0.0.1:8080/object/astaxie1373349756660423900`
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!