Commit 62d5d8479d804ef4041e20c138a612b353b12795

Authored by zhangqijia
1 parent 729bc257

http服务只用写actionStruct

@@ -16,7 +16,7 @@ func main() { @@ -16,7 +16,7 @@ func main() {
16 signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) 16 signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
17 17
18 web := net.NewHttpServer("v1") 18 web := net.NewHttpServer("v1")
19 - web.BindHandler(&actions.HttpAction{HttpServer: web}) 19 + web.BindHandler(&actions.AccountAction{HttpServer: web})
20 go func() { 20 go func() {
21 err <- web.Start() 21 err <- web.Start()
22 }() 22 }()
1 -Subproject commit 432c66d9981fa1f6423f3e5aa27543a98ce8895c 1 +Subproject commit a2411bbcfc631805718ef4d692a42ba0200e698c
src/actions/HttpAction.go renamed to src/actions/AccountAction.go
@@ -2,7 +2,6 @@ package actions @@ -2,7 +2,6 @@ package actions
2 2
3 import ( 3 import (
4 "github.com/gin-gonic/gin" 4 "github.com/gin-gonic/gin"
5 - "net/http"  
6 "pro2d/conf" 5 "pro2d/conf"
7 "pro2d/protos/pb" 6 "pro2d/protos/pb"
8 "pro2d/src/components/net" 7 "pro2d/src/components/net"
@@ -10,53 +9,42 @@ import ( @@ -10,53 +9,42 @@ import (
10 "pro2d/src/utils" 9 "pro2d/src/utils"
11 ) 10 )
12 11
13 -type HttpAction struct { 12 +type AccountAction struct {
14 HttpServer *net.HttpServer 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 var register pb.Register 17 var register pb.Register
24 if err := c.ShouldBindJSON(&register); err != nil { 18 if err := c.ShouldBindJSON(&register); err != nil {
25 - PubRsp(c, -1, err.Error())  
26 - return 19 + return -1, err.Error()
27 } 20 }
28 21
29 account := models.NewAccount(register.Phone) 22 account := models.NewAccount(register.Phone)
30 if err := account.Load(); err == nil { 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 account.Uid = conf.SnowFlack.NextValStr() 27 account.Uid = conf.SnowFlack.NextValStr()
36 account.Password = utils.Md5V(register.Password) 28 account.Password = utils.Md5V(register.Password)
37 if _, err := account.Create(); err != nil{ 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 account.Password = register.Password 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 var login pb.Account 37 var login pb.Account
47 if err := c.ShouldBindJSON(&login); err != nil { 38 if err := c.ShouldBindJSON(&login); err != nil {
48 - PubRsp(c, -1, err.Error())  
49 - return 39 + return -1, err.Error()
50 } 40 }
51 account := models.NewAccount(login.Phone) 41 account := models.NewAccount(login.Phone)
52 if err := account.Load(); err != nil { 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 if utils.Md5V(login.Password) != account.Password { 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 var gs []*pb.ServiceInfo 50 var gs []*pb.ServiceInfo
@@ -71,5 +59,5 @@ func (h *HttpAction) Login(c *gin.Context) { @@ -71,5 +59,5 @@ func (h *HttpAction) Login(c *gin.Context) {
71 Uid: account.Uid, 59 Uid: account.Uid,
72 GameService: gs, 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,6 +3,7 @@ package net
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "github.com/gin-gonic/gin" 5 "github.com/gin-gonic/gin"
  6 + "net/http"
6 "pro2d/conf" 7 "pro2d/conf"
7 "pro2d/src/components/db" 8 "pro2d/src/components/db"
8 "pro2d/src/components/etcd" 9 "pro2d/src/components/etcd"
@@ -32,7 +33,11 @@ func NewHttpServer(version string, port ...string) *HttpServer { @@ -32,7 +33,11 @@ func NewHttpServer(version string, port ...string) *HttpServer {
32 func (h *HttpServer)HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { 33 func (h *HttpServer)HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc {
33 return func(c *gin.Context) { 34 return func(c *gin.Context) {
34 v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(c)}) 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
src/components/net/http_test.go 0 → 100644
@@ -0,0 +1,22 @@ @@ -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 \ No newline at end of file 23 \ No newline at end of file