Skip to content

Commit efd1d58

Browse files
committed
<案例><beego><API案例>
1 parent 15f7452 commit efd1d58

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+38425
-0
lines changed

demo/apiproject/conf/app.conf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
appname = apiproject
2+
httpport = 8080
3+
runmode = dev
4+
autorender = false
5+
copyrequestbody = true
6+
EnableDocs = true

demo/apiproject/controllers/object.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package controllers
2+
3+
import (
4+
"iceinto/apiproject/models"
5+
"encoding/json"
6+
7+
"github.com/astaxie/beego"
8+
)
9+
10+
// Operations about object
11+
type ObjectController struct {
12+
beego.Controller
13+
}
14+
15+
// @Title Create
16+
// @Description create object
17+
// @Param body body models.Object true "The object content"
18+
// @Success 200 {string} models.Object.Id
19+
// @Failure 403 body is empty
20+
// @router / [post]
21+
func (o *ObjectController) Post() {
22+
var ob models.Object
23+
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
24+
objectid := models.AddOne(ob)
25+
o.Data["json"] = map[string]string{"ObjectId": objectid}
26+
o.ServeJSON()
27+
}
28+
29+
// @Title Get
30+
// @Description find object by objectid
31+
// @Param objectId path string true "the objectid you want to get"
32+
// @Success 200 {object} models.Object
33+
// @Failure 403 :objectId is empty
34+
// @router /:objectId [get]
35+
func (o *ObjectController) Get() {
36+
objectId := o.Ctx.Input.Param(":objectId")
37+
if objectId != "" {
38+
ob, err := models.GetOne(objectId)
39+
if err != nil {
40+
o.Data["json"] = err.Error()
41+
} else {
42+
o.Data["json"] = ob
43+
}
44+
}
45+
o.ServeJSON()
46+
}
47+
48+
// @Title GetAll
49+
// @Description get all objects
50+
// @Success 200 {object} models.Object
51+
// @Failure 403 :objectId is empty
52+
// @router / [get]
53+
func (o *ObjectController) GetAll() {
54+
obs := models.GetAll()
55+
o.Data["json"] = obs
56+
o.ServeJSON()
57+
}
58+
59+
// @Title 更改
60+
// @Description 更改主要模块
61+
// @Param objectId path string true "The objectid you want to update"
62+
// @Param body body models.Object true "The body"
63+
// @Success 200 {object} models.Object
64+
// @Failure 403 :objectId is empty
65+
// @router /:objectId [put]
66+
func (o *ObjectController) Put() {
67+
objectId := o.Ctx.Input.Param(":objectId")
68+
var ob models.Object
69+
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
70+
71+
err := models.Update(objectId, ob.Score)
72+
if err != nil {
73+
o.Data["json"] = err.Error()
74+
} else {
75+
o.Data["json"] = "update success!"
76+
}
77+
o.ServeJSON()
78+
}
79+
80+
// @Title Delete
81+
// @Description delete the object
82+
// @Param objectId path string true "The objectId you want to delete"
83+
// @Success 200 {string} delete success!
84+
// @Failure 403 objectId is empty
85+
// @router /:objectId [delete]
86+
func (o *ObjectController) Delete() {
87+
objectId := o.Ctx.Input.Param(":objectId")
88+
models.Delete(objectId)
89+
o.Data["json"] = "delete success!"
90+
o.ServeJSON()
91+
}
92+

demo/apiproject/controllers/user.go

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package controllers
2+
3+
import (
4+
"iceinto/apiproject/models"
5+
"encoding/json"
6+
7+
"github.com/astaxie/beego"
8+
)
9+
10+
// Operations about Users
11+
type UserController struct {
12+
beego.Controller
13+
}
14+
15+
// @Title CreateUser
16+
// @Description create users
17+
// @Param body body models.User true "body for user content"
18+
// @Success 200 {int} models.User.Id
19+
// @Failure 403 body is empty
20+
// @router / [post]
21+
func (u *UserController) Post() {
22+
var user models.User
23+
json.Unmarshal(u.Ctx.Input.RequestBody, &user)
24+
uid := models.AddUser(user)
25+
u.Data["json"] = map[string]string{"uid": uid}
26+
u.ServeJSON()
27+
}
28+
29+
// @Title GetAll
30+
// @Description get all Users
31+
// @Success 200 {object} models.User
32+
// @router / [get]
33+
func (u *UserController) GetAll() {
34+
users := models.GetAllUsers()
35+
u.Data["json"] = users
36+
u.ServeJSON()
37+
}
38+
39+
// @Title Get
40+
// @Description get user by uid
41+
// @Param uid path string true "The key for staticblock"
42+
// @Success 200 {object} models.User
43+
// @Failure 403 :uid is empty
44+
// @router /:uid [get]
45+
func (u *UserController) Get() {
46+
uid := u.GetString(":uid")
47+
if uid != "" {
48+
user, err := models.GetUser(uid)
49+
if err != nil {
50+
u.Data["json"] = err.Error()
51+
} else {
52+
u.Data["json"] = user
53+
}
54+
}
55+
u.ServeJSON()
56+
}
57+
58+
// @Title Update
59+
// @Description update the user
60+
// @Param uid path string true "The uid you want to update"
61+
// @Param body body models.User true "body for user content"
62+
// @Success 200 {object} models.User
63+
// @Failure 403 :uid is not int
64+
// @router /:uid [put]
65+
func (u *UserController) Put() {
66+
uid := u.GetString(":uid")
67+
if uid != "" {
68+
var user models.User
69+
json.Unmarshal(u.Ctx.Input.RequestBody, &user)
70+
uu, err := models.UpdateUser(uid, &user)
71+
if err != nil {
72+
u.Data["json"] = err.Error()
73+
} else {
74+
u.Data["json"] = uu
75+
}
76+
}
77+
u.ServeJSON()
78+
}
79+
80+
// @Title Delete
81+
// @Description delete the user
82+
// @Param uid path string true "The uid you want to delete"
83+
// @Success 200 {string} delete success!
84+
// @Failure 403 uid is empty
85+
// @router /:uid [delete]
86+
func (u *UserController) Delete() {
87+
uid := u.GetString(":uid")
88+
models.DeleteUser(uid)
89+
u.Data["json"] = "delete success!"
90+
u.ServeJSON()
91+
}
92+
93+
// @Title Login
94+
// @Description Logs user into the system
95+
// @Param username query string true "The username for login"
96+
// @Param password query string true "The password for login"
97+
// @Success 200 {string} login success
98+
// @Failure 403 user not exist
99+
// @router /login [get]
100+
func (u *UserController) Login() {
101+
username := u.GetString("username")
102+
password := u.GetString("password")
103+
if models.Login(username, password) {
104+
u.Data["json"] = "login success"
105+
} else {
106+
u.Data["json"] = "user not exist"
107+
}
108+
u.ServeJSON()
109+
}
110+
111+
// @Title logout
112+
// @Description Logs out current logged in user session
113+
// @Success 200 {string} logout success
114+
// @router /logout [get]
115+
func (u *UserController) Logout() {
116+
u.Data["json"] = "logout success"
117+
u.ServeJSON()
118+
}
119+

demo/apiproject/lastupdate.tmp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"/Users/iceinto/Documents/go/src/iceinto/apiproject/controllers":1494307756000000000}

demo/apiproject/logs/project.log

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2017/05/09 13:38:43 [I] [asm_amd64.s:2197] http server Running on http://:8080
2+
2017/05/09 13:38:48 [D] [server.go:2568] | 192.168.1.237| 404 | 536.214µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
3+
2017/05/09 13:38:53 [D] [server.go:2568] | 192.168.1.237| 404 | 294.496µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
4+
2017/05/09 13:38:55 [I] [asm_amd64.s:2197] http server Running on http://:8080
5+
2017/05/09 13:39:41 [I] [asm_amd64.s:2197] http server Running on http://:8080
6+
2017/05/09 13:39:48 [D] [server.go:2568] | 192.168.1.237| 404 | 537.996µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
7+
2017/05/09 13:39:53 [D] [server.go:2568] | 192.168.1.237| 404 | 283.94µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
8+
2017/05/09 13:40:48 [D] [server.go:2568] | 192.168.1.237| 404 | 301.091µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
9+
2017/05/09 13:40:50 [I] [asm_amd64.s:2197] http server Running on http://:8080
10+
2017/05/09 13:40:54 [D] [server.go:2568] | 192.168.1.237| 404 | 655.687µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
11+
2017/05/09 13:41:48 [D] [server.go:2568] | 192.168.1.237| 404 | 190.367µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
12+
2017/05/09 13:41:53 [D] [server.go:2568] | 192.168.1.237| 404 | 201.953µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
13+
2017/05/09 13:42:48 [D] [server.go:2568] | 192.168.1.237| 404 | 287.159µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
14+
2017/05/09 13:42:54 [D] [server.go:2568] | 192.168.1.237| 404 | 264.999µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
15+
2017/05/09 13:43:23 [D] [server.go:2568] | 127.0.0.1| 200 | 172.511µs| match| GET  /v1/object r:/v1/object/
16+
2017/05/09 13:43:48 [D] [server.go:2568] | 192.168.1.237| 404 | 286.923µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
17+
2017/05/09 13:43:54 [D] [server.go:2568] | 192.168.1.237| 404 | 250.187µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
18+
2017/05/09 13:44:23 [I] [asm_amd64.s:2197] http server Running on http://:8080
19+
2017/05/09 13:44:48 [D] [server.go:2568] | 192.168.1.237| 404 | 8.645628ms| nomatch| GET  /DevMgmt/DiscoveryTree.xml
20+
2017/05/09 13:44:56 [D] [server.go:2568] | 192.168.1.237| 404 | 295.941µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
21+
2017/05/09 13:45:47 [D] [server.go:2568] | 192.168.1.237| 404 | 324.441µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
22+
2017/05/09 13:45:53 [D] [server.go:2568] | 192.168.1.237| 404 | 237.673µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
23+
2017/05/09 13:46:48 [D] [server.go:2568] | 192.168.1.237| 404 | 259.678µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
24+
2017/05/09 13:46:53 [D] [server.go:2568] | 192.168.1.237| 404 | 184.708µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
25+
2017/05/09 13:47:48 [D] [server.go:2568] | 192.168.1.237| 404 | 295.851µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
26+
2017/05/09 13:47:53 [D] [server.go:2568] | 192.168.1.237| 404 | 265.542µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
27+
2017/05/09 13:48:47 [D] [server.go:2568] | 192.168.1.237| 404 | 225.265µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
28+
2017/05/09 13:48:53 [D] [server.go:2568] | 192.168.1.237| 404 | 271.222µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
29+
2017/05/09 13:49:48 [D] [server.go:2568] | 192.168.1.237| 404 | 284.038µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
30+
2017/05/09 13:49:54 [D] [server.go:2568] | 192.168.1.237| 404 | 210.788µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml
31+
2017/05/11 19:46:55 [I] [asm_amd64.s:2197] http server Running on http://:8080
32+
2017/05/11 20:01:50 [D] [server.go:2568] | 192.168.1.237| 404 | 575.543µs| nomatch| GET  /DevMgmt/DiscoveryTree.xml

demo/apiproject/main.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
_ "iceinto/apiproject/routers"
5+
"github.com/astaxie/beego/logs"
6+
"github.com/astaxie/beego"
7+
"fmt"
8+
)
9+
10+
func main() {
11+
fmt.Println("当前运行模式:" + beego.BConfig.RunMode)
12+
if beego.BConfig.RunMode == "dev" {
13+
beego.BConfig.WebConfig.DirectoryIndex = true
14+
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
15+
}
16+
logs.EnableFuncCallDepth(true)
17+
logs.Async(1e3)
18+
logs.SetLogger("console")
19+
logs.SetLevel(7)
20+
logs.SetLogger(logs.AdapterFile,`{"filename":"logs/project.log","level":7,"maxlines":0,"maxsize":0,"daily":true,"maxdays":30}`)
21+
22+
beego.Run()
23+
}

demo/apiproject/models/object.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

0 commit comments

Comments
 (0)