Blame view

actions/HttpAction.go 3.12 KB
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
1
2
3
4
5
6
7
  package actions
  
  import (
  	"fmt"
  	"github.com/gin-gonic/gin"
  	"net/http"
  	"pro2d/components/db"
c92a54a3   zhangqijia   循环引用的问题
8
  	"pro2d/components/etcd"
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
9
10
11
12
13
14
15
16
17
18
19
  	"pro2d/conf"
  	"pro2d/models"
  	"pro2d/protos/pb"
  	"pro2d/utils"
  	"reflect"
  	"strings"
  )
  
  type HttpAction struct {
  	version string
  	port []string
c92a54a3   zhangqijia   循环引用的问题
20
  	EtcdClient *etcd.EtcdClient
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  }
  
  func NewHttpAction(version string, port ...string) *HttpAction {
  	return &HttpAction{version: version, port: port}
  }
  
  func Pong (c *gin.Context) {
  	c.JSON(200, gin.H{
  		"message": "pong",
  	})
  }
  
  func HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc {
  	apiFun := func(c *gin.Context) interface{} { return c }
  	return func(c *gin.Context) {
  		tvl.Call([]reflect.Value{obj, reflect.ValueOf(apiFun(c))})
  	}
  }
  
  func GetRoutePath(objName, objFunc string) string  {
  	return strings.ToLower(objName + "/" + objFunc)
  }
  
  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(&register); 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
  	}
  
a0c46341   zhangqijia   fix: http login
88
  	var gs []*pb.ServiceInfo
c92a54a3   zhangqijia   循环引用的问题
89
  	for k, v := range h.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) {
a0c46341   zhangqijia   fix: http login
90
91
92
93
94
95
96
97
98
99
100
  		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)
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
101
102
103
104
105
106
107
108
  }
  
  func (h *HttpAction) Start() error {
  	//mongo 初始化
  	db.MongoDatabase = db.MongoClient.Database(conf.GlobalConf.AccountConf.DBName)
  	models.InitAccountServerModels()
  
  	//Etcd 初始化
c92a54a3   zhangqijia   循环引用的问题
109
110
  	h.EtcdClient = etcd.NewEtcdClient(conf.GlobalConf.Etcd)
  	h.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.AccountConf.Name, conf.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.AccountConf.IP, conf.GlobalConf.AccountConf.Port), 5)
9644352a   zhangqijia   登录服改为http,游戏服改为长连...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  
  	//gin初始化
  	r := gin.Default()
  	r.GET("/ping", Pong)
  	typ := reflect.TypeOf(h)
  	val := reflect.ValueOf(h)
  	//t := reflect.Indirect(val).Type()
  	//objectName := t.Name()
  
  	numOfMethod := val.NumMethod()
  	for i := 0; i < numOfMethod; i++ {
  		method := typ.Method(i)
  		r.GET(GetRoutePath(h.version, method.Name), HandlerFuncObj(method.Func, val))
  		r.POST(GetRoutePath(h.version, method.Name), HandlerFuncObj(method.Func, val))
  	}
  	return r.Run(h.port...) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
  }