HttpAction.go
2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package actions
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"pro2d/components/db"
"pro2d/conf"
"pro2d/models"
"pro2d/protos/pb"
"pro2d/utils"
"reflect"
"strings"
)
type HttpAction struct {
version string
port []string
}
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(®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
}
PubRsp(c, 0, account.Account)
}
func (h *HttpAction) Start() error {
//mongo 初始化
db.MongoDatabase = db.MongoClient.Database(conf.GlobalConf.AccountConf.DBName)
models.InitAccountServerModels()
//Etcd 初始化
conf.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.AccountConf.Name, conf.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.AccountConf.IP, conf.GlobalConf.AccountConf.Port), 5)
//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")
}