Commit 62d5d8479d804ef4041e20c138a612b353b12795
1 parent
729bc257
http服务只用写actionStruct
Showing
5 changed files
with
41 additions
and
26 deletions
Show diff stats
cmd/http.go
... | ... | @@ -16,7 +16,7 @@ func main() { |
16 | 16 | signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) |
17 | 17 | |
18 | 18 | web := net.NewHttpServer("v1") |
19 | - web.BindHandler(&actions.HttpAction{HttpServer: web}) | |
19 | + web.BindHandler(&actions.AccountAction{HttpServer: web}) | |
20 | 20 | go func() { |
21 | 21 | err <- web.Start() |
22 | 22 | }() | ... | ... |
src/actions/HttpAction.go renamed to src/actions/AccountAction.go
... | ... | @@ -2,7 +2,6 @@ package actions |
2 | 2 | |
3 | 3 | import ( |
4 | 4 | "github.com/gin-gonic/gin" |
5 | - "net/http" | |
6 | 5 | "pro2d/conf" |
7 | 6 | "pro2d/protos/pb" |
8 | 7 | "pro2d/src/components/net" |
... | ... | @@ -10,53 +9,42 @@ import ( |
10 | 9 | "pro2d/src/utils" |
11 | 10 | ) |
12 | 11 | |
13 | -type HttpAction struct { | |
12 | +type AccountAction struct { | |
14 | 13 | HttpServer *net.HttpServer |
15 | 14 | } |
16 | 15 | |
17 | - | |
18 | -func PubRsp(c *gin.Context, code int, data interface{}) { | |
19 | - c.JSON(http.StatusOK, gin.H{"code": code, "data": data}) | |
20 | -} | |
21 | - | |
22 | -func (h *HttpAction) Register(c *gin.Context) { | |
16 | +func (h *AccountAction) Register(c *gin.Context) (int, interface{}){ | |
23 | 17 | var register pb.Register |
24 | 18 | if err := c.ShouldBindJSON(®ister); err != nil { |
25 | - PubRsp(c, -1, err.Error()) | |
26 | - return | |
19 | + return -1, err.Error() | |
27 | 20 | } |
28 | 21 | |
29 | 22 | account := models.NewAccount(register.Phone) |
30 | 23 | if err := account.Load(); err == nil { |
31 | - PubRsp(c, -2, "account exist: " + register.Phone) | |
32 | - return | |
24 | + return -2 , "account exist: " + register.Phone | |
33 | 25 | } |
34 | 26 | |
35 | 27 | account.Uid = conf.SnowFlack.NextValStr() |
36 | 28 | account.Password = utils.Md5V(register.Password) |
37 | 29 | if _, err := account.Create(); err != nil{ |
38 | - PubRsp(c, -3, "account register err: " + err.Error()) | |
39 | - return | |
30 | + return -3, "account register err: " + err.Error() | |
40 | 31 | } |
41 | 32 | account.Password = register.Password |
42 | - PubRsp(c, 0, account.Account) | |
33 | + return 0, account.Account | |
43 | 34 | } |
44 | 35 | |
45 | -func (h *HttpAction) Login(c *gin.Context) { | |
36 | +func (h *AccountAction) Login(c *gin.Context) (int,interface{}) { | |
46 | 37 | var login pb.Account |
47 | 38 | if err := c.ShouldBindJSON(&login); err != nil { |
48 | - PubRsp(c, -1, err.Error()) | |
49 | - return | |
39 | + return -1, err.Error() | |
50 | 40 | } |
51 | 41 | account := models.NewAccount(login.Phone) |
52 | 42 | if err := account.Load(); err != nil { |
53 | - PubRsp(c, -2, err.Error()) | |
54 | - return | |
43 | + return -2, err.Error() | |
55 | 44 | } |
56 | 45 | |
57 | 46 | if utils.Md5V(login.Password) != account.Password { |
58 | - PubRsp(c, -3, "password error") | |
59 | - return | |
47 | + return -3, "password error" | |
60 | 48 | } |
61 | 49 | |
62 | 50 | var gs []*pb.ServiceInfo |
... | ... | @@ -71,5 +59,5 @@ func (h *HttpAction) Login(c *gin.Context) { |
71 | 59 | Uid: account.Uid, |
72 | 60 | GameService: gs, |
73 | 61 | } |
74 | - PubRsp(c, 0, rsp) | |
62 | + return 0, rsp | |
75 | 63 | } | ... | ... |
src/components/net/http.go
... | ... | @@ -3,6 +3,7 @@ package net |
3 | 3 | import ( |
4 | 4 | "fmt" |
5 | 5 | "github.com/gin-gonic/gin" |
6 | + "net/http" | |
6 | 7 | "pro2d/conf" |
7 | 8 | "pro2d/src/components/db" |
8 | 9 | "pro2d/src/components/etcd" |
... | ... | @@ -32,7 +33,11 @@ func NewHttpServer(version string, port ...string) *HttpServer { |
32 | 33 | func (h *HttpServer)HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { |
33 | 34 | return func(c *gin.Context) { |
34 | 35 | v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(c)}) |
35 | - fmt.Printf("v : %v\n", v) | |
36 | + if len(v) != 2 { | |
37 | + c.JSON(http.StatusNotFound, gin.H{"code": -100, "data": ""}) | |
38 | + return | |
39 | + } | |
40 | + c.JSON(http.StatusOK, gin.H{"code": v[0].Interface(), "data": v[1].Interface()}) | |
36 | 41 | } |
37 | 42 | } |
38 | 43 | ... | ... |
... | ... | @@ -0,0 +1,22 @@ |
1 | +package net | |
2 | + | |
3 | +import ( | |
4 | + "github.com/gin-gonic/gin" | |
5 | + "testing" | |
6 | +) | |
7 | + | |
8 | +type HttpAction struct { | |
9 | +} | |
10 | + | |
11 | +func (h *HttpAction) PrintA(c *gin.Context) (int, interface{}) { | |
12 | + return 0, "I'm A" | |
13 | +} | |
14 | +func (h *HttpAction) PrintB(c *gin.Context) (int, interface{}) { | |
15 | + return 0, "I'm B" | |
16 | +} | |
17 | + | |
18 | +func TestHttpServer_Start(t *testing.T) { | |
19 | + web := NewHttpServer("v1") | |
20 | + web.BindHandler(&HttpAction{}) | |
21 | + web.Start() | |
22 | +} | |
0 | 23 | \ No newline at end of file | ... | ... |