diff --git a/.tree.txt.swp b/.tree.txt.swp new file mode 100644 index 0000000..58f8d1c Binary files /dev/null and b/.tree.txt.swp differ diff --git a/README.md b/README.md index 03c456d..ac65f00 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,33 @@ * proto + models 查询 * redis +## 目录结构 +```text +. +├── bin +├── cmd //入口文件 +├── conf //配置文件 +├── csvdata //csv的golang struct +├── doc //文档 +├── docker //docker部署 +├── protos +│   ├── doc //协议文档 +│   └── pb //生成的pb.go文件 +├── src //逻辑目录 +│   ├── actions //逻辑处理 +│   ├── common +│   ├── components //组件 +│   │   ├── db +│   │   ├── etcd +│   │   ├── jwt +│   │   ├── logger +│   │   └── net //网络组件(http + tcp) +│   ├── models //数据模型 +│   ├── plugin //逻辑热更的插件 +│   └── utils //通用函数 +├── test //测试 +├── tools //工具(proto自动生成协议号,csv自动生成struct) +``` ## 环境安装 protoc-go-inject-tag: 目的是往protos文件中打入自定义标签 @@ -34,6 +61,7 @@ $ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc ``` ## 文档 [证书制作](doc/cret.md) +[包组成结构](doc/proto.md) ## Usage diff --git a/cmd/http.go b/cmd/http.go index c800c19..9e1355a 100644 --- a/cmd/http.go +++ b/cmd/http.go @@ -6,6 +6,7 @@ import ( _ "pro2d/conf" "pro2d/src/actions" "pro2d/src/components/logger" + "pro2d/src/components/net" "syscall" ) @@ -14,7 +15,8 @@ func main() { stopChan := make(chan os.Signal) signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) - web := actions.NewHttpAction("v1") + web := net.NewHttpServer("v1") + web.BindHandler(&actions.HttpAction{HttpServer: web}) go func() { err <- web.Start() }() diff --git a/src/actions/HttpAction.go b/src/actions/HttpAction.go index 32fc9d7..9457bbb 100644 --- a/src/actions/HttpAction.go +++ b/src/actions/HttpAction.go @@ -1,45 +1,19 @@ package actions import ( - "fmt" "github.com/gin-gonic/gin" "net/http" "pro2d/conf" "pro2d/protos/pb" - "pro2d/src/components/db" - "pro2d/src/components/etcd" + "pro2d/src/components/net" "pro2d/src/models" "pro2d/src/utils" - "reflect" - "strings" ) type HttpAction struct { - version string - port []string - EtcdClient *etcd.EtcdClient + HttpServer *net.HttpServer } -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}) @@ -86,7 +60,7 @@ func (h *HttpAction) Login(c *gin.Context) { } var gs []*pb.ServiceInfo - for k, v := range h.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) { + for k, v := range h.HttpServer.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) { gs = append(gs, &pb.ServiceInfo{ Id: k, Name: conf.GlobalConf.GameConf.Name, @@ -99,33 +73,3 @@ func (h *HttpAction) Login(c *gin.Context) { } PubRsp(c, 0, rsp) } - -func (h *HttpAction) Start() error { - //mongo 初始化 - db.MongoDatabase = db.MongoClient.Database(conf.GlobalConf.AccountConf.DBName) - models.InitAccountServerModels() - - //Etcd 初始化 - var err error - h.EtcdClient, err = etcd.NewEtcdClient(conf.GlobalConf.Etcd) - if err != nil { - return err - } - h.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") -} \ No newline at end of file diff --git a/src/components/net/http.go b/src/components/net/http.go new file mode 100644 index 0000000..be4217c --- /dev/null +++ b/src/components/net/http.go @@ -0,0 +1,75 @@ +package net + +import ( + "fmt" + "github.com/gin-gonic/gin" + "pro2d/conf" + "pro2d/src/components/db" + "pro2d/src/components/etcd" + "pro2d/src/models" + "reflect" + "strings" +) + +type HttpServer struct { + version string + port []string + EtcdClient *etcd.EtcdClient + + Handler interface{} +} + +func Pong (c *gin.Context) { + c.JSON(200, gin.H{ + "message": "pong", + }) +} + +func NewHttpServer(version string, port ...string) *HttpServer { + return &HttpServer{version: version, port: port} +} + +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) + } +} + +func GetRoutePath(objName, objFunc string) string { + return strings.ToLower(objName + "/" + objFunc) +} + +func (h *HttpServer) BindHandler(handler interface{}) { + h.Handler = handler +} + +func (h *HttpServer) Start() error { + //mongo 初始化 + db.MongoDatabase = db.MongoClient.Database(conf.GlobalConf.AccountConf.DBName) + models.InitAccountServerModels() + + //Etcd 初始化 + var err error + h.EtcdClient, err = etcd.NewEtcdClient(conf.GlobalConf.Etcd) + if err != nil { + return err + } + h.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.Handler) + val := reflect.ValueOf(h.Handler) + //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), h.HandlerFuncObj(method.Func, val)) + r.POST(GetRoutePath(h.version, method.Name), h.HandlerFuncObj(method.Func, val)) + } + return r.Run(h.port...) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") +} \ No newline at end of file -- libgit2 0.21.2