fece5adc by astaxie

add example for api application

1 parent 7bfb4126
1 appname = beeapi
2 httpport = 8080
3 runmode = dev
4 autorender = false
5 copyrequestbody = true
1 package controllers
2
3 import (
4 "encoding/json"
5 "github.com/astaxie/beego"
6 "github.com/astaxie/beego/example/beeapi/models"
7 )
8
9 type ResponseInfo struct {
10 }
11
12 type ObejctController struct {
13 beego.Controller
14 }
15
16 func (this *ObejctController) Post() {
17 var ob models.Object
18 json.Unmarshal(this.Ctx.RequestBody, &ob)
19 objectid := models.AddOne(ob)
20 this.Data["json"] = "{\"ObjectId\":\"" + objectid + "\"}"
21 this.ServeJson()
22 }
23
24 func (this *ObejctController) Get() {
25 objectId := this.Ctx.Params[":objectId"]
26 if objectId != "" {
27 ob, err := models.GetOne(objectId)
28 if err != nil {
29 this.Data["json"] = err
30 } else {
31 this.Data["json"] = ob
32 }
33 } else {
34 obs := models.GetAll()
35 this.Data["json"] = obs
36 }
37 this.ServeJson()
38 }
39
40 func (this *ObejctController) Put() {
41 objectId := this.Ctx.Params[":objectId"]
42 var ob models.Object
43 json.Unmarshal(this.Ctx.RequestBody, &ob)
44
45 err := models.Update(objectId, ob.Score)
46 if err != nil {
47 this.Data["json"] = err
48 } else {
49 this.Data["json"] = "update success!"
50 }
51 this.ServeJson()
52 }
53
54 func (this *ObejctController) Delete() {
55 objectId := this.Ctx.Params[":objectId"]
56 models.Delete(objectId)
57 this.Data["json"] = "delete success!"
58 this.ServeJson()
59 }
1 package main
2
3 import (
4 "github.com/astaxie/beego"
5 "github.com/astaxie/beego/example/beeapi/controllers"
6 )
7
8 // Objects
9
10 // URL HTTP Verb Functionality
11 // /object POST Creating Objects
12 // /object/<objectId> GET Retrieving Objects
13 // /object/<objectId> PUT Updating Objects
14 // /object GET Queries
15 // /object/<objectId> DELETE Deleting Objects
16
17 func main() {
18 beego.RESTRouter("/object", &controllers.ObejctController{})
19 beego.Run()
20 }
1 package models
2
3 import (
4 "errors"
5 "strconv"
6 "time"
7 )
8
9 var (
10 Objects map[string]*Object
11 )
12
13 type Object struct {
14 ObjectId string
15 Score int64
16 PlayerName string
17 }
18
19 func init() {
20 Objects = make(map[string]*Object)
21 Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"}
22 Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"}
23 }
24
25 func AddOne(object Object) (ObjectId string) {
26 object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
27 Objects[object.ObjectId] = &object
28 return object.ObjectId
29 }
30
31 func GetOne(ObjectId string) (object *Object, err error) {
32 if v, ok := Objects[ObjectId]; ok {
33 return v, nil
34 }
35 return nil, errors.New("ObjectId Not Exist")
36 }
37
38 func GetAll() map[string]*Object {
39 return Objects
40 }
41
42 func Update(ObjectId string, Score int64) (err error) {
43 if v, ok := Objects[ObjectId]; ok {
44 v.Score = Score
45 return nil
46 }
47 return errors.New("ObjectId Not Exist")
48 }
49
50 func Delete(ObjectId string) {
51 delete(Objects, ObjectId)
52 }
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!