diff --git a/cmd/http.go b/cmd/http.go index 9e1355a..f7bf928 100644 --- a/cmd/http.go +++ b/cmd/http.go @@ -16,7 +16,7 @@ func main() { signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) web := net.NewHttpServer("v1") - web.BindHandler(&actions.HttpAction{HttpServer: web}) + web.BindHandler(&actions.AccountAction{HttpServer: web}) go func() { err <- web.Start() }() diff --git a/protos b/protos index 432c66d..a2411bb 160000 --- a/protos +++ b/protos @@ -1 +1 @@ -Subproject commit 432c66d9981fa1f6423f3e5aa27543a98ce8895c +Subproject commit a2411bbcfc631805718ef4d692a42ba0200e698c diff --git a/src/actions/AccountAction.go b/src/actions/AccountAction.go new file mode 100644 index 0000000..a2e3680 --- /dev/null +++ b/src/actions/AccountAction.go @@ -0,0 +1,63 @@ +package actions + +import ( + "github.com/gin-gonic/gin" + "pro2d/conf" + "pro2d/protos/pb" + "pro2d/src/components/net" + "pro2d/src/models" + "pro2d/src/utils" +) + +type AccountAction struct { + HttpServer *net.HttpServer +} + +func (h *AccountAction) Register(c *gin.Context) (int, interface{}){ + var register pb.Register + if err := c.ShouldBindJSON(®ister); err != nil { + return -1, err.Error() + } + + account := models.NewAccount(register.Phone) + if err := account.Load(); err == nil { + return -2 , "account exist: " + register.Phone + } + + account.Uid = conf.SnowFlack.NextValStr() + account.Password = utils.Md5V(register.Password) + if _, err := account.Create(); err != nil{ + return -3, "account register err: " + err.Error() + } + account.Password = register.Password + return 0, account.Account +} + +func (h *AccountAction) Login(c *gin.Context) (int,interface{}) { + var login pb.Account + if err := c.ShouldBindJSON(&login); err != nil { + return -1, err.Error() + } + account := models.NewAccount(login.Phone) + if err := account.Load(); err != nil { + return -2, err.Error() + } + + if utils.Md5V(login.Password) != account.Password { + return -3, "password error" + } + + var gs []*pb.ServiceInfo + for k, v := range h.HttpServer.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) { + gs = append(gs, &pb.ServiceInfo{ + Id: k, + Name: conf.GlobalConf.GameConf.Name, + Address: v, + }) + } + rsp := &pb.LoginRsp{ + Uid: account.Uid, + GameService: gs, + } + return 0, rsp +} diff --git a/src/actions/HttpAction.go b/src/actions/HttpAction.go deleted file mode 100644 index 9457bbb..0000000 --- a/src/actions/HttpAction.go +++ /dev/null @@ -1,75 +0,0 @@ -package actions - -import ( - "github.com/gin-gonic/gin" - "net/http" - "pro2d/conf" - "pro2d/protos/pb" - "pro2d/src/components/net" - "pro2d/src/models" - "pro2d/src/utils" -) - -type HttpAction struct { - HttpServer *net.HttpServer -} - - -func PubRsp(c *gin.Context, code int, data interface{}) { - c.JSON(http.StatusOK, gin.H{"code": code, "data": data}) -} - -func (h *HttpAction) Register(c *gin.Context) { - var register pb.Register - if err := c.ShouldBindJSON(®ister); err != nil { - PubRsp(c, -1, err.Error()) - return - } - - account := models.NewAccount(register.Phone) - if err := account.Load(); err == nil { - PubRsp(c, -2, "account exist: " + register.Phone) - return - } - - account.Uid = conf.SnowFlack.NextValStr() - account.Password = utils.Md5V(register.Password) - if _, err := account.Create(); err != nil{ - PubRsp(c, -3, "account register err: " + err.Error()) - return - } - account.Password = register.Password - PubRsp(c, 0, account.Account) -} - -func (h *HttpAction) Login(c *gin.Context) { - var login pb.Account - if err := c.ShouldBindJSON(&login); err != nil { - PubRsp(c, -1, err.Error()) - return - } - account := models.NewAccount(login.Phone) - if err := account.Load(); err != nil { - PubRsp(c, -2, err.Error()) - return - } - - if utils.Md5V(login.Password) != account.Password { - PubRsp(c, -3, "password error") - return - } - - var gs []*pb.ServiceInfo - for k, v := range h.HttpServer.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) { - gs = append(gs, &pb.ServiceInfo{ - Id: k, - Name: conf.GlobalConf.GameConf.Name, - Address: v, - }) - } - rsp := &pb.LoginRsp{ - Uid: account.Uid, - GameService: gs, - } - PubRsp(c, 0, rsp) -} diff --git a/src/components/net/http.go b/src/components/net/http.go index be4217c..79d2966 100644 --- a/src/components/net/http.go +++ b/src/components/net/http.go @@ -3,6 +3,7 @@ package net import ( "fmt" "github.com/gin-gonic/gin" + "net/http" "pro2d/conf" "pro2d/src/components/db" "pro2d/src/components/etcd" @@ -32,7 +33,11 @@ func NewHttpServer(version string, port ...string) *HttpServer { func (h *HttpServer)HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { return func(c *gin.Context) { v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(c)}) - fmt.Printf("v : %v\n", v) + if len(v) != 2 { + c.JSON(http.StatusNotFound, gin.H{"code": -100, "data": ""}) + return + } + c.JSON(http.StatusOK, gin.H{"code": v[0].Interface(), "data": v[1].Interface()}) } } diff --git a/src/components/net/http_test.go b/src/components/net/http_test.go new file mode 100644 index 0000000..a2d05c9 --- /dev/null +++ b/src/components/net/http_test.go @@ -0,0 +1,22 @@ +package net + +import ( + "github.com/gin-gonic/gin" + "testing" +) + +type HttpAction struct { +} + +func (h *HttpAction) PrintA(c *gin.Context) (int, interface{}) { + return 0, "I'm A" +} +func (h *HttpAction) PrintB(c *gin.Context) (int, interface{}) { + return 0, "I'm B" +} + +func TestHttpServer_Start(t *testing.T) { + web := NewHttpServer("v1") + web.BindHandler(&HttpAction{}) + web.Start() +} \ No newline at end of file -- libgit2 0.21.2