Commit 252ca9a0bc479c21efb6acfa23c8bef61a28904e

Authored by zhangqijia
1 parent 3f0a1991

解释目录结构

.tree.txt.swp 0 → 100644
No preview for this file type
@@ -18,6 +18,33 @@ @@ -18,6 +18,33 @@
18 * proto + models 查询 18 * proto + models 查询
19 * redis 19 * redis
20 20
  21 +## 目录结构
  22 +```text
  23 +.
  24 +├── bin
  25 +├── cmd //入口文件
  26 +├── conf //配置文件
  27 +├── csvdata //csv的golang struct
  28 +├── doc //文档
  29 +├── docker //docker部署
  30 +├── protos
  31 +│   ├── doc //协议文档
  32 +│   └── pb //生成的pb.go文件
  33 +├── src //逻辑目录
  34 +│   ├── actions //逻辑处理
  35 +│   ├── common
  36 +│   ├── components //组件
  37 +│   │   ├── db
  38 +│   │   ├── etcd
  39 +│   │   ├── jwt
  40 +│   │   ├── logger
  41 +│   │   └── net //网络组件(http + tcp)
  42 +│   ├── models //数据模型
  43 +│   ├── plugin //逻辑热更的插件
  44 +│   └── utils //通用函数
  45 +├── test //测试
  46 +├── tools //工具(proto自动生成协议号,csv自动生成struct)
  47 +```
21 48
22 ## 环境安装 49 ## 环境安装
23 protoc-go-inject-tag: 目的是往protos文件中打入自定义标签 50 protoc-go-inject-tag: 目的是往protos文件中打入自定义标签
@@ -34,6 +61,7 @@ $ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc @@ -34,6 +61,7 @@ $ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
34 ``` 61 ```
35 ## 文档 62 ## 文档
36 [证书制作](doc/cret.md) 63 [证书制作](doc/cret.md)
  64 +[包组成结构](doc/proto.md)
37 65
38 66
39 ## Usage 67 ## Usage
@@ -6,6 +6,7 @@ import ( @@ -6,6 +6,7 @@ import (
6 _ "pro2d/conf" 6 _ "pro2d/conf"
7 "pro2d/src/actions" 7 "pro2d/src/actions"
8 "pro2d/src/components/logger" 8 "pro2d/src/components/logger"
  9 + "pro2d/src/components/net"
9 "syscall" 10 "syscall"
10 ) 11 )
11 12
@@ -14,7 +15,8 @@ func main() { @@ -14,7 +15,8 @@ func main() {
14 stopChan := make(chan os.Signal) 15 stopChan := make(chan os.Signal)
15 signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) 16 signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
16 17
17 - web := actions.NewHttpAction("v1") 18 + web := net.NewHttpServer("v1")
  19 + web.BindHandler(&actions.HttpAction{HttpServer: web})
18 go func() { 20 go func() {
19 err <- web.Start() 21 err <- web.Start()
20 }() 22 }()
src/actions/HttpAction.go
1 package actions 1 package actions
2 2
3 import ( 3 import (
4 - "fmt"  
5 "github.com/gin-gonic/gin" 4 "github.com/gin-gonic/gin"
6 "net/http" 5 "net/http"
7 "pro2d/conf" 6 "pro2d/conf"
8 "pro2d/protos/pb" 7 "pro2d/protos/pb"
9 - "pro2d/src/components/db"  
10 - "pro2d/src/components/etcd" 8 + "pro2d/src/components/net"
11 "pro2d/src/models" 9 "pro2d/src/models"
12 "pro2d/src/utils" 10 "pro2d/src/utils"
13 - "reflect"  
14 - "strings"  
15 ) 11 )
16 12
17 type HttpAction struct { 13 type HttpAction struct {
18 - version string  
19 - port []string  
20 - EtcdClient *etcd.EtcdClient 14 + HttpServer *net.HttpServer
21 } 15 }
22 16
23 -func NewHttpAction(version string, port ...string) *HttpAction {  
24 - return &HttpAction{version: version, port: port}  
25 -}  
26 -  
27 -func Pong (c *gin.Context) {  
28 - c.JSON(200, gin.H{  
29 - "message": "pong",  
30 - })  
31 -}  
32 -  
33 -func HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc {  
34 - apiFun := func(c *gin.Context) interface{} { return c }  
35 - return func(c *gin.Context) {  
36 - tvl.Call([]reflect.Value{obj, reflect.ValueOf(apiFun(c))})  
37 - }  
38 -}  
39 -  
40 -func GetRoutePath(objName, objFunc string) string {  
41 - return strings.ToLower(objName + "/" + objFunc)  
42 -}  
43 17
44 func PubRsp(c *gin.Context, code int, data interface{}) { 18 func PubRsp(c *gin.Context, code int, data interface{}) {
45 c.JSON(http.StatusOK, gin.H{"code": code, "data": data}) 19 c.JSON(http.StatusOK, gin.H{"code": code, "data": data})
@@ -86,7 +60,7 @@ func (h *HttpAction) Login(c *gin.Context) { @@ -86,7 +60,7 @@ func (h *HttpAction) Login(c *gin.Context) {
86 } 60 }
87 61
88 var gs []*pb.ServiceInfo 62 var gs []*pb.ServiceInfo
89 - for k, v := range h.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) { 63 + for k, v := range h.HttpServer.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) {
90 gs = append(gs, &pb.ServiceInfo{ 64 gs = append(gs, &pb.ServiceInfo{
91 Id: k, 65 Id: k,
92 Name: conf.GlobalConf.GameConf.Name, 66 Name: conf.GlobalConf.GameConf.Name,
@@ -99,33 +73,3 @@ func (h *HttpAction) Login(c *gin.Context) { @@ -99,33 +73,3 @@ func (h *HttpAction) Login(c *gin.Context) {
99 } 73 }
100 PubRsp(c, 0, rsp) 74 PubRsp(c, 0, rsp)
101 } 75 }
102 -  
103 -func (h *HttpAction) Start() error {  
104 - //mongo 初始化  
105 - db.MongoDatabase = db.MongoClient.Database(conf.GlobalConf.AccountConf.DBName)  
106 - models.InitAccountServerModels()  
107 -  
108 - //Etcd 初始化  
109 - var err error  
110 - h.EtcdClient, err = etcd.NewEtcdClient(conf.GlobalConf.Etcd)  
111 - if err != nil {  
112 - return err  
113 - }  
114 - h.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.AccountConf.Name, conf.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.AccountConf.IP, conf.GlobalConf.AccountConf.Port), 5)  
115 -  
116 - //gin初始化  
117 - r := gin.Default()  
118 - r.GET("/ping", Pong)  
119 - typ := reflect.TypeOf(h)  
120 - val := reflect.ValueOf(h)  
121 - //t := reflect.Indirect(val).Type()  
122 - //objectName := t.Name()  
123 -  
124 - numOfMethod := val.NumMethod()  
125 - for i := 0; i < numOfMethod; i++ {  
126 - method := typ.Method(i)  
127 - r.GET(GetRoutePath(h.version, method.Name), HandlerFuncObj(method.Func, val))  
128 - r.POST(GetRoutePath(h.version, method.Name), HandlerFuncObj(method.Func, val))  
129 - }  
130 - return r.Run(h.port...) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")  
131 -}  
132 \ No newline at end of file 76 \ No newline at end of file
src/components/net/http.go 0 → 100644
@@ -0,0 +1,75 @@ @@ -0,0 +1,75 @@
  1 +package net
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/gin-gonic/gin"
  6 + "pro2d/conf"
  7 + "pro2d/src/components/db"
  8 + "pro2d/src/components/etcd"
  9 + "pro2d/src/models"
  10 + "reflect"
  11 + "strings"
  12 +)
  13 +
  14 +type HttpServer struct {
  15 + version string
  16 + port []string
  17 + EtcdClient *etcd.EtcdClient
  18 +
  19 + Handler interface{}
  20 +}
  21 +
  22 +func Pong (c *gin.Context) {
  23 + c.JSON(200, gin.H{
  24 + "message": "pong",
  25 + })
  26 +}
  27 +
  28 +func NewHttpServer(version string, port ...string) *HttpServer {
  29 + return &HttpServer{version: version, port: port}
  30 +}
  31 +
  32 +func (h *HttpServer)HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc {
  33 + return func(c *gin.Context) {
  34 + v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(c)})
  35 + fmt.Printf("v : %v\n", v)
  36 + }
  37 +}
  38 +
  39 +func GetRoutePath(objName, objFunc string) string {
  40 + return strings.ToLower(objName + "/" + objFunc)
  41 +}
  42 +
  43 +func (h *HttpServer) BindHandler(handler interface{}) {
  44 + h.Handler = handler
  45 +}
  46 +
  47 +func (h *HttpServer) Start() error {
  48 + //mongo 初始化
  49 + db.MongoDatabase = db.MongoClient.Database(conf.GlobalConf.AccountConf.DBName)
  50 + models.InitAccountServerModels()
  51 +
  52 + //Etcd 初始化
  53 + var err error
  54 + h.EtcdClient, err = etcd.NewEtcdClient(conf.GlobalConf.Etcd)
  55 + if err != nil {
  56 + return err
  57 + }
  58 + h.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.AccountConf.Name, conf.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.AccountConf.IP, conf.GlobalConf.AccountConf.Port), 5)
  59 +
  60 + //gin初始化
  61 + r := gin.Default()
  62 + r.GET("/ping", Pong)
  63 + typ := reflect.TypeOf(h.Handler)
  64 + val := reflect.ValueOf(h.Handler)
  65 + //t := reflect.Indirect(val).Type()
  66 + //objectName := t.Name()
  67 +
  68 + numOfMethod := val.NumMethod()
  69 + for i := 0; i < numOfMethod; i++ {
  70 + method := typ.Method(i)
  71 + r.GET(GetRoutePath(h.version, method.Name), h.HandlerFuncObj(method.Func, val))
  72 + r.POST(GetRoutePath(h.version, method.Name), h.HandlerFuncObj(method.Func, val))
  73 + }
  74 + return r.Run(h.port...) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
  75 +}
0 \ No newline at end of file 76 \ No newline at end of file