Commit 3592dfd3747ba9d086918986319e82dac1745af3
1 parent
eb417b0b
重构models, 索引唯一索引
Showing
20 changed files
with
239 additions
and
49 deletions
Show diff stats
Makefile
@@ -4,6 +4,7 @@ all: ge build run | @@ -4,6 +4,7 @@ all: ge build run | ||
4 | 4 | ||
5 | ge: | 5 | ge: |
6 | protoc -I./protos --go_out=./protos --go-grpc_out=./protos ./protos/*proto | 6 | protoc -I./protos --go_out=./protos --go-grpc_out=./protos ./protos/*proto |
7 | + protoc-go-inject-tag -input=./protos/pb/*.pb.go | ||
7 | 8 | ||
8 | test: | 9 | test: |
9 | go run test/client.go | 10 | go run test/client.go |
README.md
@@ -12,6 +12,11 @@ | @@ -12,6 +12,11 @@ | ||
12 | * proto gorm 查询 | 12 | * proto gorm 查询 |
13 | 13 | ||
14 | ## 环境安装 | 14 | ## 环境安装 |
15 | +protoc-go-inject-tag: 目的是往protos文件中打入自定义标签 | ||
16 | +```shell | ||
17 | +$ go get github.com/favadi/protoc-go-inject-tag | ||
18 | +``` | ||
19 | + | ||
15 | etcd | 20 | etcd |
16 | ```shell | 21 | ```shell |
17 | $ go get go.etcd.io/etcd/client/v3 | 22 | $ go get go.etcd.io/etcd/client/v3 |
actions/accountaction.go
@@ -10,7 +10,7 @@ import ( | @@ -10,7 +10,7 @@ import ( | ||
10 | ) | 10 | ) |
11 | 11 | ||
12 | func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (*pb.PubRsp, error) { | 12 | func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (*pb.PubRsp, error) { |
13 | - ok, account := models.AccountExistByPhone(s.DBName, in.Phone) | 13 | + ok, account := models.AccountExistByPhone(in.Phone) |
14 | if !ok { | 14 | if !ok { |
15 | account.Phone = in.Phone | 15 | account.Phone = in.Phone |
16 | account.Password = utils.Md5V(in.Password) | 16 | account.Password = utils.Md5V(in.Password) |
actions/basic.go
@@ -3,7 +3,9 @@ package actions | @@ -3,7 +3,9 @@ package actions | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "net" | 5 | "net" |
6 | + "pro2d/components/db" | ||
6 | "pro2d/conf" | 7 | "pro2d/conf" |
8 | + "pro2d/utils" | ||
7 | ) | 9 | ) |
8 | 10 | ||
9 | type BasicServer struct { | 11 | type BasicServer struct { |
@@ -17,6 +19,12 @@ func NewServer(db string) *BasicServer { | @@ -17,6 +19,12 @@ func NewServer(db string) *BasicServer { | ||
17 | } | 19 | } |
18 | 20 | ||
19 | func (b *BasicServer) Start(sConf *conf.SConf) (net.Listener, error) { | 21 | func (b *BasicServer) Start(sConf *conf.SConf) (net.Listener, error) { |
22 | + //初始化数据库 | ||
23 | + err := db.Connect(conf.GlobalConf.MongoConf.User, conf.GlobalConf.MongoConf.Password, conf.GlobalConf.MongoConf.Host, conf.GlobalConf.MongoConf.Port, conf.GlobalConf.MongoConf.MaxNum, conf.GlobalConf.MongoConf.TimeOut, sConf.DBName) | ||
24 | + if err != nil { | ||
25 | + utils.Sugar.Errorf("mongodb init error: %v", err) | ||
26 | + } | ||
27 | + | ||
20 | listing := fmt.Sprintf(":%d", sConf.Port) | 28 | listing := fmt.Sprintf(":%d", sConf.Port) |
21 | lis, err := net.Listen("tcp", listing) | 29 | lis, err := net.Listen("tcp", listing) |
22 | if err != nil { | 30 | if err != nil { |
actions/server.go
@@ -5,6 +5,7 @@ import ( | @@ -5,6 +5,7 @@ import ( | ||
5 | "google.golang.org/grpc" | 5 | "google.golang.org/grpc" |
6 | "google.golang.org/grpc/reflection" | 6 | "google.golang.org/grpc/reflection" |
7 | "pro2d/conf" | 7 | "pro2d/conf" |
8 | + "pro2d/models" | ||
8 | "pro2d/protos/pb" | 9 | "pro2d/protos/pb" |
9 | "pro2d/utils" | 10 | "pro2d/utils" |
10 | ) | 11 | ) |
@@ -35,6 +36,8 @@ func (s *AccountServer)Start() error { | @@ -35,6 +36,8 @@ func (s *AccountServer)Start() error { | ||
35 | return err | 36 | return err |
36 | } | 37 | } |
37 | 38 | ||
39 | + models.InitAccountServerModels() | ||
40 | + | ||
38 | //new一个grpc | 41 | //new一个grpc |
39 | gs := grpc.NewServer(grpc.UnaryInterceptor(AccountServerInterceptor)) | 42 | gs := grpc.NewServer(grpc.UnaryInterceptor(AccountServerInterceptor)) |
40 | 43 | ||
@@ -52,7 +55,6 @@ func (s *AccountServer)Stop() { | @@ -52,7 +55,6 @@ func (s *AccountServer)Stop() { | ||
52 | s.BasicServer.Close() | 55 | s.BasicServer.Close() |
53 | } | 56 | } |
54 | 57 | ||
55 | - | ||
56 | type GameServer struct{ | 58 | type GameServer struct{ |
57 | pb.UnimplementedGameServer | 59 | pb.UnimplementedGameServer |
58 | *BasicServer | 60 | *BasicServer |
@@ -78,6 +80,8 @@ func (s *GameServer)Start() error { | @@ -78,6 +80,8 @@ func (s *GameServer)Start() error { | ||
78 | return err | 80 | return err |
79 | } | 81 | } |
80 | 82 | ||
83 | + models.InitGameServerModels() | ||
84 | + | ||
81 | //new一个grpc | 85 | //new一个grpc |
82 | gs := grpc.NewServer(grpc.UnaryInterceptor(GameServerInterceptor)) | 86 | gs := grpc.NewServer(grpc.UnaryInterceptor(GameServerInterceptor)) |
83 | 87 |
@@ -0,0 +1,17 @@ | @@ -0,0 +1,17 @@ | ||
1 | +package main | ||
2 | + | ||
3 | +import ( | ||
4 | + "pro2d/actions" | ||
5 | + "pro2d/utils" | ||
6 | +) | ||
7 | + | ||
8 | +func main() { | ||
9 | + err := make(chan error) | ||
10 | + server := actions.NewAccountServer() | ||
11 | + go func() { | ||
12 | + defer server.Stop() | ||
13 | + err <- server.Start() | ||
14 | + }() | ||
15 | + | ||
16 | + utils.Sugar.Errorf("server error: %v", <- err) | ||
17 | +} |
@@ -0,0 +1,19 @@ | @@ -0,0 +1,19 @@ | ||
1 | +package main | ||
2 | + | ||
3 | +import ( | ||
4 | + "pro2d/actions" | ||
5 | + "pro2d/utils" | ||
6 | +) | ||
7 | + | ||
8 | +func main() { | ||
9 | + err := make(chan error) | ||
10 | + | ||
11 | + | ||
12 | + server := actions.NewGameServer() | ||
13 | + go func() { | ||
14 | + defer server.Stop() | ||
15 | + err <- server.Start() | ||
16 | + }() | ||
17 | + | ||
18 | + utils.Sugar.Errorf("server error: %v", <- err) | ||
19 | +} |
components/db/mongo.go
@@ -8,14 +8,18 @@ import ( | @@ -8,14 +8,18 @@ import ( | ||
8 | "go.mongodb.org/mongo-driver/mongo/options" | 8 | "go.mongodb.org/mongo-driver/mongo/options" |
9 | "go.mongodb.org/mongo-driver/mongo/readpref" | 9 | "go.mongodb.org/mongo-driver/mongo/readpref" |
10 | "go.mongodb.org/mongo-driver/x/bsonx" | 10 | "go.mongodb.org/mongo-driver/x/bsonx" |
11 | + "sort" | ||
11 | "strconv" | 12 | "strconv" |
12 | "time" | 13 | "time" |
13 | ) | 14 | ) |
14 | 15 | ||
15 | -var MongoDBClient *mongo.Client | 16 | +var ( |
17 | + MongoClient *mongo.Client | ||
18 | + MongoDatabase *mongo.Database | ||
19 | +) | ||
16 | 20 | ||
17 | //初始化 | 21 | //初始化 |
18 | -func Connect(user, password, host string,port int, MaxNum int, timeOut int) error { | 22 | +func Connect(user, password, host string,port int, MaxNum int, timeOut int, dbname string) error { |
19 | var uri string | 23 | var uri string |
20 | if user!= "" { | 24 | if user!= "" { |
21 | //uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s?w=majority", conf.User, conf.Password, conf.Host, conf.Port, conf.DBName) | 25 | //uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s?w=majority", conf.User, conf.Password, conf.Host, conf.Port, conf.DBName) |
@@ -33,19 +37,42 @@ func Connect(user, password, host string,port int, MaxNum int, timeOut int) erro | @@ -33,19 +37,42 @@ func Connect(user, password, host string,port int, MaxNum int, timeOut int) erro | ||
33 | o.SetMaxPoolSize(uint64(MaxNum)) | 37 | o.SetMaxPoolSize(uint64(MaxNum)) |
34 | // 发起链接 | 38 | // 发起链接 |
35 | var err error | 39 | var err error |
36 | - MongoDBClient, err = mongo.Connect(ctx, o) | 40 | + MongoClient , err = mongo.Connect(ctx, o) |
37 | if err != nil { | 41 | if err != nil { |
38 | return err | 42 | return err |
39 | } | 43 | } |
40 | // 判断服务是不是可用 | 44 | // 判断服务是不是可用 |
41 | - if err = MongoDBClient.Ping(context.Background(), readpref.Primary()); err != nil { | 45 | + if err = MongoClient.Ping(context.Background(), readpref.Primary()); err != nil { |
42 | return err | 46 | return err |
43 | } | 47 | } |
48 | + | ||
49 | + MongoDatabase = MongoClient.Database(dbname) | ||
44 | return nil | 50 | return nil |
45 | } | 51 | } |
46 | 52 | ||
53 | +func CreateCollection(collection string) error { | ||
54 | + colls, _ := MongoDatabase.ListCollectionNames(context.TODO(), bson.D{}) | ||
55 | + pos := sort.SearchStrings(colls, collection) | ||
56 | + if pos != len(colls) { | ||
57 | + if collection == colls[pos] { | ||
58 | + return MongoDatabase.CreateCollection(context.TODO(), collection) | ||
59 | + } | ||
60 | + } | ||
61 | + return MongoDatabase.CreateCollection(context.TODO(), collection) | ||
62 | +} | ||
63 | + | ||
64 | +func SetUnique(collection string, key string) (string, error) { | ||
65 | + return MongoDatabase.Collection(collection).Indexes().CreateOne( | ||
66 | + context.TODO(), | ||
67 | + mongo.IndexModel{ | ||
68 | + Keys : bsonx.Doc{{key, bsonx.Int32(1)}}, | ||
69 | + Options: options.Index().SetUnique(true), | ||
70 | + }, | ||
71 | + ) | ||
72 | +} | ||
73 | + | ||
47 | type MgoColl struct { | 74 | type MgoColl struct { |
48 | - mgo *mongo.Collection | 75 | + collection *mongo.Collection |
49 | 76 | ||
50 | pri interface{} | 77 | pri interface{} |
51 | schema interface{} | 78 | schema interface{} |
@@ -58,9 +85,9 @@ func GetBsonM(key string, value interface{}) interface{} { | @@ -58,9 +85,9 @@ func GetBsonM(key string, value interface{}) interface{} { | ||
58 | return bson.M{key: value} | 85 | return bson.M{key: value} |
59 | } | 86 | } |
60 | 87 | ||
61 | -func NewMongoColl(database, collection string, pri, schema interface{}) *MgoColl { | 88 | +func NewMongoColl(collection string, pri, schema interface{}) *MgoColl { |
62 | return &MgoColl{ | 89 | return &MgoColl{ |
63 | - mgo: MongoDBClient.Database(database).Collection(collection), | 90 | + collection: MongoDatabase.Collection(collection), |
64 | 91 | ||
65 | pri: pri, | 92 | pri: pri, |
66 | schema: schema, | 93 | schema: schema, |
@@ -75,19 +102,19 @@ func (m *MgoColl)SetDatabase(databases string) { | @@ -75,19 +102,19 @@ func (m *MgoColl)SetDatabase(databases string) { | ||
75 | func (m *MgoColl) FindOneKV(key string, value interface{}) *mongo.SingleResult { | 102 | func (m *MgoColl) FindOneKV(key string, value interface{}) *mongo.SingleResult { |
76 | //collection. | 103 | //collection. |
77 | filter := bson.D{ {key, value}} | 104 | filter := bson.D{ {key, value}} |
78 | - singleResult := m.mgo.FindOne(context.TODO(), filter) | 105 | + singleResult := m.collection.FindOne(context.TODO(), filter) |
79 | return singleResult | 106 | return singleResult |
80 | } | 107 | } |
81 | 108 | ||
82 | func (m *MgoColl) FindOne(pri interface{}) *mongo.SingleResult { | 109 | func (m *MgoColl) FindOne(pri interface{}) *mongo.SingleResult { |
83 | //collection. | 110 | //collection. |
84 | - singleResult := m.mgo.FindOne(context.TODO(), pri) | 111 | + singleResult := m.collection.FindOne(context.TODO(), pri) |
85 | return singleResult | 112 | return singleResult |
86 | } | 113 | } |
87 | 114 | ||
88 | //插入单个 | 115 | //插入单个 |
89 | func (m *MgoColl) InsertOne(value interface{}) *mongo.InsertOneResult { | 116 | func (m *MgoColl) InsertOne(value interface{}) *mongo.InsertOneResult { |
90 | - insertResult, err := m.mgo.InsertOne(context.TODO(), value) | 117 | + insertResult, err := m.collection.InsertOne(context.TODO(), value) |
91 | if err != nil { | 118 | if err != nil { |
92 | fmt.Println(err) | 119 | fmt.Println(err) |
93 | } | 120 | } |
@@ -96,8 +123,8 @@ func (m *MgoColl) InsertOne(value interface{}) *mongo.InsertOneResult { | @@ -96,8 +123,8 @@ func (m *MgoColl) InsertOne(value interface{}) *mongo.InsertOneResult { | ||
96 | 123 | ||
97 | //查询集合里有多少数据 | 124 | //查询集合里有多少数据 |
98 | func (m *MgoColl) CollectionCount() (string, int64) { | 125 | func (m *MgoColl) CollectionCount() (string, int64) { |
99 | - size, _ := m.mgo.EstimatedDocumentCount(context.TODO()) | ||
100 | - return m.mgo.Name(), size | 126 | + size, _ := m.collection.EstimatedDocumentCount(context.TODO()) |
127 | + return m.collection.Name(), size | ||
101 | } | 128 | } |
102 | 129 | ||
103 | //按选项查询集合 Skip 跳过 Limit 读取数量 sort 1 ,-1 . 1 为最初时间读取 , -1 为最新时间读取 | 130 | //按选项查询集合 Skip 跳过 Limit 读取数量 sort 1 ,-1 . 1 为最初时间读取 , -1 为最新时间读取 |
@@ -108,7 +135,7 @@ func (m *MgoColl) CollectionDocuments(Skip, Limit int64, sort int) *mongo.Cursor | @@ -108,7 +135,7 @@ func (m *MgoColl) CollectionDocuments(Skip, Limit int64, sort int) *mongo.Cursor | ||
108 | {}} | 135 | {}} |
109 | findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip) | 136 | findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip) |
110 | //findOptions.SetLimit(i) | 137 | //findOptions.SetLimit(i) |
111 | - temp, _ := m.mgo.Find(context.Background(), filter, findOptions) | 138 | + temp, _ := m.collection.Find(context.Background(), filter, findOptions) |
112 | return temp | 139 | return temp |
113 | } | 140 | } |
114 | 141 | ||
@@ -126,8 +153,8 @@ func (m *MgoColl) ParsingId(result string) (time.Time, uint64) { | @@ -126,8 +153,8 @@ func (m *MgoColl) ParsingId(result string) (time.Time, uint64) { | ||
126 | func (m *MgoColl) DeleteAndFind(key string, value interface{}) (int64, *mongo.SingleResult) { | 153 | func (m *MgoColl) DeleteAndFind(key string, value interface{}) (int64, *mongo.SingleResult) { |
127 | filter := bson.D{ | 154 | filter := bson.D{ |
128 | {key, value}} | 155 | {key, value}} |
129 | - singleResult := m.mgo.FindOne(context.TODO(), filter) | ||
130 | - DeleteResult, err := m.mgo.DeleteOne(context.TODO(), filter, nil) | 156 | + singleResult := m.collection.FindOne(context.TODO(), filter) |
157 | + DeleteResult, err := m.collection.DeleteOne(context.TODO(), filter, nil) | ||
131 | if err != nil { | 158 | if err != nil { |
132 | fmt.Println("删除时出现错误,你删不掉的~") | 159 | fmt.Println("删除时出现错误,你删不掉的~") |
133 | } | 160 | } |
@@ -137,7 +164,7 @@ func (m *MgoColl) DeleteAndFind(key string, value interface{}) (int64, *mongo.Si | @@ -137,7 +164,7 @@ func (m *MgoColl) DeleteAndFind(key string, value interface{}) (int64, *mongo.Si | ||
137 | //删除文章 | 164 | //删除文章 |
138 | func (m *MgoColl) Delete(key string, value interface{}) int64 { | 165 | func (m *MgoColl) Delete(key string, value interface{}) int64 { |
139 | filter := bson.D{ {key, value}} | 166 | filter := bson.D{ {key, value}} |
140 | - count, err := m.mgo.DeleteOne(context.TODO(), filter, nil) | 167 | + count, err := m.collection.DeleteOne(context.TODO(), filter, nil) |
141 | if err != nil { | 168 | if err != nil { |
142 | fmt.Println(err) | 169 | fmt.Println(err) |
143 | } | 170 | } |
@@ -149,7 +176,7 @@ func (m *MgoColl) Delete(key string, value interface{}) int64 { | @@ -149,7 +176,7 @@ func (m *MgoColl) Delete(key string, value interface{}) int64 { | ||
149 | func (m *MgoColl) DeleteMany(key string, value interface{}) int64 { | 176 | func (m *MgoColl) DeleteMany(key string, value interface{}) int64 { |
150 | filter := bson.D{ {key, value}} | 177 | filter := bson.D{ {key, value}} |
151 | 178 | ||
152 | - count, err := m.mgo.DeleteMany(context.TODO(), filter) | 179 | + count, err := m.collection.DeleteMany(context.TODO(), filter) |
153 | if err != nil { | 180 | if err != nil { |
154 | fmt.Println(err) | 181 | fmt.Println(err) |
155 | } | 182 | } |
@@ -157,8 +184,8 @@ func (m *MgoColl) DeleteMany(key string, value interface{}) int64 { | @@ -157,8 +184,8 @@ func (m *MgoColl) DeleteMany(key string, value interface{}) int64 { | ||
157 | } | 184 | } |
158 | 185 | ||
159 | //索引 | 186 | //索引 |
160 | -func (m *MgoColl) Index(key string){ | ||
161 | - m.mgo.Indexes().CreateOne( | 187 | +func (m *MgoColl) SetUnique(key string){ |
188 | + m.collection.Indexes().CreateOne( | ||
162 | context.Background(), | 189 | context.Background(), |
163 | mongo.IndexModel{ | 190 | mongo.IndexModel{ |
164 | Keys : bsonx.Doc{{key, bsonx.Int32(1)}}, | 191 | Keys : bsonx.Doc{{key, bsonx.Int32(1)}}, |
@@ -172,7 +199,7 @@ func (m *MgoColl) FindOneAndUpdate(filter interface{}, update interface{})*mongo | @@ -172,7 +199,7 @@ func (m *MgoColl) FindOneAndUpdate(filter interface{}, update interface{})*mongo | ||
172 | //filter := bson.M{"name": "x", "array.name": "b"} | 199 | //filter := bson.M{"name": "x", "array.name": "b"} |
173 | //update := bson.M{"array.$[item].detail": "test"} | 200 | //update := bson.M{"array.$[item].detail": "test"} |
174 | 201 | ||
175 | - res := m.mgo.FindOneAndUpdate(context.Background(), | 202 | + res := m.collection.FindOneAndUpdate(context.Background(), |
176 | filter, | 203 | filter, |
177 | bson.M{"$set": update}) | 204 | bson.M{"$set": update}) |
178 | if res.Err() != nil { | 205 | if res.Err() != nil { |
@@ -182,7 +209,7 @@ func (m *MgoColl) FindOneAndUpdate(filter interface{}, update interface{})*mongo | @@ -182,7 +209,7 @@ func (m *MgoColl) FindOneAndUpdate(filter interface{}, update interface{})*mongo | ||
182 | } | 209 | } |
183 | 210 | ||
184 | func (m *MgoColl) UpdateOne(filter interface{}, update interface{})*mongo.UpdateResult { | 211 | func (m *MgoColl) UpdateOne(filter interface{}, update interface{})*mongo.UpdateResult { |
185 | - res, err := m.mgo.UpdateOne(context.TODO(), filter, update) | 212 | + res, err := m.collection.UpdateOne(context.TODO(), filter, update) |
186 | if err != nil { | 213 | if err != nil { |
187 | return nil | 214 | return nil |
188 | } | 215 | } |
conf/conf.go
@@ -5,7 +5,6 @@ import ( | @@ -5,7 +5,6 @@ import ( | ||
5 | lumberjack "gopkg.in/natefinch/lumberjack.v2" | 5 | lumberjack "gopkg.in/natefinch/lumberjack.v2" |
6 | "gopkg.in/yaml.v3" | 6 | "gopkg.in/yaml.v3" |
7 | "io/ioutil" | 7 | "io/ioutil" |
8 | - "pro2d/components/db" | ||
9 | "pro2d/utils" | 8 | "pro2d/utils" |
10 | ) | 9 | ) |
11 | 10 | ||
@@ -74,9 +73,4 @@ func init() { | @@ -74,9 +73,4 @@ func init() { | ||
74 | //初始化雪花算法 | 73 | //初始化雪花算法 |
75 | SnowFlack = utils.NewSnowflake(GlobalConf.WorkerID, GlobalConf.DatacenterID) | 74 | SnowFlack = utils.NewSnowflake(GlobalConf.WorkerID, GlobalConf.DatacenterID) |
76 | 75 | ||
77 | - //初始化数据库 | ||
78 | - err = db.Connect(GlobalConf.MongoConf.User, GlobalConf.MongoConf.Password, GlobalConf.MongoConf.Host, GlobalConf.MongoConf.Port, GlobalConf.MongoConf.MaxNum, GlobalConf.MongoConf.TimeOut) | ||
79 | - if err != nil { | ||
80 | - utils.Sugar.Errorf("mongodb init error: %v", err) | ||
81 | - } | ||
82 | } | 76 | } |
83 | \ No newline at end of file | 77 | \ No newline at end of file |
@@ -4,9 +4,7 @@ go 1.17 | @@ -4,9 +4,7 @@ go 1.17 | ||
4 | 4 | ||
5 | require ( | 5 | require ( |
6 | github.com/dgrijalva/jwt-go v3.2.0+incompatible | 6 | github.com/dgrijalva/jwt-go v3.2.0+incompatible |
7 | - github.com/garyburd/redigo v1.6.3 | ||
8 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b | 7 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b |
9 | - github.com/golang/protobuf v1.5.2 | ||
10 | go.mongodb.org/mongo-driver v1.8.3 | 8 | go.mongodb.org/mongo-driver v1.8.3 |
11 | go.uber.org/zap v1.17.0 | 9 | go.uber.org/zap v1.17.0 |
12 | google.golang.org/grpc v1.38.0 | 10 | google.golang.org/grpc v1.38.0 |
@@ -16,7 +14,10 @@ require ( | @@ -16,7 +14,10 @@ require ( | ||
16 | ) | 14 | ) |
17 | 15 | ||
18 | require ( | 16 | require ( |
17 | + github.com/favadi/protoc-go-inject-tag v1.3.0 // indirect | ||
18 | + github.com/garyburd/redigo v1.6.3 // indirect | ||
19 | github.com/go-stack/stack v1.8.0 // indirect | 19 | github.com/go-stack/stack v1.8.0 // indirect |
20 | + github.com/golang/protobuf v1.5.2 // indirect | ||
20 | github.com/golang/snappy v0.0.1 // indirect | 21 | github.com/golang/snappy v0.0.1 // indirect |
21 | github.com/klauspost/compress v1.13.6 // indirect | 22 | github.com/klauspost/compress v1.13.6 // indirect |
22 | github.com/pkg/errors v0.9.1 // indirect | 23 | github.com/pkg/errors v0.9.1 // indirect |
@@ -13,6 +13,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF | @@ -13,6 +13,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF | ||
13 | github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= | 13 | github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= |
14 | github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= | 14 | github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= |
15 | github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= | 15 | github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= |
16 | +github.com/favadi/protoc-go-inject-tag v1.3.0 h1:JPrmsmc/uBShG85uY5xGZIa5WJ0IaNZn6LZhQR9tIQE= | ||
17 | +github.com/favadi/protoc-go-inject-tag v1.3.0/go.mod h1:SSkUBgfqw2IJ2p7NPNKWk0Idwxt/qIt2LQgFPUgRGtc= | ||
16 | github.com/garyburd/redigo v1.6.3 h1:HCeeRluvAgMusMomi1+6Y5dmFOdYV/JzoRrrbFlkGIc= | 18 | github.com/garyburd/redigo v1.6.3 h1:HCeeRluvAgMusMomi1+6Y5dmFOdYV/JzoRrrbFlkGIc= |
17 | github.com/garyburd/redigo v1.6.3/go.mod h1:rTb6epsqigu3kYKBnaF028A7Tf/Aw5s0cqA47doKKqw= | 19 | github.com/garyburd/redigo v1.6.3/go.mod h1:rTb6epsqigu3kYKBnaF028A7Tf/Aw5s0cqA47doKKqw= |
18 | github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= | 20 | github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= |
models/account.go
@@ -10,20 +10,20 @@ type AccountModel struct { | @@ -10,20 +10,20 @@ type AccountModel struct { | ||
10 | *pb.AccountInfo | 10 | *pb.AccountInfo |
11 | } | 11 | } |
12 | 12 | ||
13 | -func AccountExistByPhone(database, phone string) (bool, *AccountModel){ | ||
14 | - m := NewAccount(database, phone) | 13 | +func AccountExistByPhone(phone string) (bool, *AccountModel){ |
14 | + m := NewAccount(phone) | ||
15 | if err := m.Load(); err != nil { | 15 | if err := m.Load(); err != nil { |
16 | return false, m | 16 | return false, m |
17 | } | 17 | } |
18 | return true, m | 18 | return true, m |
19 | } | 19 | } |
20 | 20 | ||
21 | -func NewAccount(database, phone string) *AccountModel { | 21 | +func NewAccount(phone string) *AccountModel { |
22 | ac := &pb.AccountInfo{ | 22 | ac := &pb.AccountInfo{ |
23 | Phone: phone, | 23 | Phone: phone, |
24 | } | 24 | } |
25 | account := &AccountModel{ | 25 | account := &AccountModel{ |
26 | - MgoColl: db.NewMongoColl(database, "account", db.GetBsonM("phone", phone), ac), | 26 | + MgoColl: db.NewMongoColl( "account", db.GetBsonM("phone", phone), ac), |
27 | AccountInfo: ac, | 27 | AccountInfo: ac, |
28 | } | 28 | } |
29 | 29 |
models/hero.go
@@ -2,7 +2,6 @@ package models | @@ -2,7 +2,6 @@ package models | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "pro2d/components/db" | 4 | "pro2d/components/db" |
5 | - "pro2d/conf" | ||
6 | "pro2d/protos/pb" | 5 | "pro2d/protos/pb" |
7 | ) | 6 | ) |
8 | 7 | ||
@@ -20,11 +19,13 @@ func GetHeros(hm HeroMap) map[string]*pb.Hero { | @@ -20,11 +19,13 @@ func GetHeros(hm HeroMap) map[string]*pb.Hero { | ||
20 | return h | 19 | return h |
21 | } | 20 | } |
22 | 21 | ||
23 | -func NewHero(h *pb.Hero) *HeroModel { | 22 | +func NewHero(id int64) *HeroModel { |
23 | + h := &pb.Hero{ | ||
24 | + Id: id, | ||
25 | + } | ||
24 | m := &HeroModel{ | 26 | m := &HeroModel{ |
25 | - MgoColl: db.NewMongoColl(conf.GlobalConf.GameConf.DBName, "hero", db.GetBsonM("id", h.Id), h), | 27 | + MgoColl: db.NewMongoColl("hero", db.GetBsonM("id", h.Id), h), |
26 | Hero: h, | 28 | Hero: h, |
27 | } | 29 | } |
28 | - m.Load() | ||
29 | return m | 30 | return m |
30 | } | 31 | } |
@@ -0,0 +1,40 @@ | @@ -0,0 +1,40 @@ | ||
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "pro2d/components/db" | ||
5 | + "pro2d/protos/pb" | ||
6 | + "pro2d/utils" | ||
7 | +) | ||
8 | + | ||
9 | +func InitDoc(schema ...interface{}) { | ||
10 | + for _, s := range schema { | ||
11 | + for coll, key := range utils.FindIndex(s) { | ||
12 | + if err := db.CreateCollection(coll); err != nil { | ||
13 | + utils.Sugar.Errorf("InitDoc err: %v", err) | ||
14 | + continue | ||
15 | + } | ||
16 | + | ||
17 | + res, err := db.SetUnique(coll, key) | ||
18 | + if err != nil { | ||
19 | + utils.Sugar.Errorf("InitDoc unique: %s, err: %v", res, err) | ||
20 | + continue | ||
21 | + } | ||
22 | + } | ||
23 | + } | ||
24 | +} | ||
25 | + | ||
26 | +func InitAccountServerModels() { | ||
27 | + var schema []interface{} = []interface{}{ | ||
28 | + pb.AccountInfo{}, | ||
29 | + } | ||
30 | + InitDoc(schema...) | ||
31 | +} | ||
32 | + | ||
33 | +func InitGameServerModels() { | ||
34 | + var schema []interface{} = []interface{}{ | ||
35 | + pb.Hero{}, | ||
36 | + pb.Role{}, | ||
37 | + pb.Team{}, | ||
38 | + } | ||
39 | + InitDoc(schema...) | ||
40 | +} | ||
0 | \ No newline at end of file | 41 | \ No newline at end of file |
@@ -0,0 +1,27 @@ | @@ -0,0 +1,27 @@ | ||
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "pro2d/components/db" | ||
6 | + "pro2d/conf" | ||
7 | + _ "pro2d/conf" | ||
8 | + "pro2d/utils" | ||
9 | + "testing" | ||
10 | +) | ||
11 | + | ||
12 | + | ||
13 | +func TestInitModels(t *testing.T) { | ||
14 | + err := db.Connect(conf.GlobalConf.MongoConf.User, conf.GlobalConf.MongoConf.Password, conf.GlobalConf.MongoConf.Host, conf.GlobalConf.MongoConf.Port, conf.GlobalConf.MongoConf.MaxNum, conf.GlobalConf.MongoConf.TimeOut,"account") | ||
15 | + if err != nil { | ||
16 | + utils.Sugar.Errorf("mongodb init error: %v", err) | ||
17 | + } | ||
18 | + | ||
19 | + InitAccountServerModels() | ||
20 | + db.MongoClient.Disconnect(context.TODO()) | ||
21 | + err = db.Connect(conf.GlobalConf.MongoConf.User, conf.GlobalConf.MongoConf.Password, conf.GlobalConf.MongoConf.Host, conf.GlobalConf.MongoConf.Port, conf.GlobalConf.MongoConf.MaxNum, conf.GlobalConf.MongoConf.TimeOut,"game") | ||
22 | + if err != nil { | ||
23 | + utils.Sugar.Errorf("mongodb init error: %v", err) | ||
24 | + } | ||
25 | + InitGameServerModels() | ||
26 | + db.MongoClient.Disconnect(context.TODO()) | ||
27 | +} | ||
0 | \ No newline at end of file | 28 | \ No newline at end of file |
models/role.go
@@ -3,7 +3,6 @@ package models | @@ -3,7 +3,6 @@ package models | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "pro2d/components/db" | 5 | "pro2d/components/db" |
6 | - "pro2d/conf" | ||
7 | "pro2d/protos/pb" | 6 | "pro2d/protos/pb" |
8 | ) | 7 | ) |
9 | 8 | ||
@@ -17,20 +16,20 @@ type RoleModel struct { | @@ -17,20 +16,20 @@ type RoleModel struct { | ||
17 | } | 16 | } |
18 | 17 | ||
19 | func RoleExistByUid(uid int64) (bool, *RoleModel){ | 18 | func RoleExistByUid(uid int64) (bool, *RoleModel){ |
20 | - m := NewRole(&pb.Role{Uid: uid}) | 19 | + m := NewRole(uid) |
21 | if err := m.Load(); err != nil { | 20 | if err := m.Load(); err != nil { |
22 | return false, m | 21 | return false, m |
23 | } | 22 | } |
24 | return true, m | 23 | return true, m |
25 | } | 24 | } |
26 | 25 | ||
27 | -func NewRole(r *pb.Role) *RoleModel { | 26 | +func NewRole(uid int64) *RoleModel { |
27 | + r := &pb.Role{Uid: uid} | ||
28 | m := &RoleModel{ | 28 | m := &RoleModel{ |
29 | - MgoColl: db.NewMongoColl(conf.GlobalConf.GameConf.DBName, "role", db.GetBsonM("uid", r.Uid), r), | 29 | + MgoColl: db.NewMongoColl("role", db.GetBsonM("uid", r.Uid), r), |
30 | Role: r, | 30 | Role: r, |
31 | Heros: make(HeroMap), | 31 | Heros: make(HeroMap), |
32 | } | 32 | } |
33 | - m.Load() | ||
34 | return m | 33 | return m |
35 | } | 34 | } |
36 | 35 | ||
@@ -41,7 +40,8 @@ func (m *RoleModel) LoadHero() { | @@ -41,7 +40,8 @@ func (m *RoleModel) LoadHero() { | ||
41 | } | 40 | } |
42 | 41 | ||
43 | func (m *RoleModel) AddHero(hero *pb.Hero) { | 42 | func (m *RoleModel) AddHero(hero *pb.Hero) { |
44 | - h := NewHero(hero) | 43 | + h := NewHero(hero.Id) |
44 | + h.Hero = hero | ||
45 | h.Create() | 45 | h.Create() |
46 | m.Heros[fmt.Sprintf("%d%d", m.Role.Id, h.Hero.Id)] = h | 46 | m.Heros[fmt.Sprintf("%d%d", m.Role.Id, h.Hero.Id)] = h |
47 | } | 47 | } |
48 | \ No newline at end of file | 48 | \ No newline at end of file |
models/role_test.go
@@ -24,12 +24,12 @@ func TestNewRole(t *testing.T) { | @@ -24,12 +24,12 @@ func TestNewRole(t *testing.T) { | ||
24 | }) | 24 | }) |
25 | role.Save() | 25 | role.Save() |
26 | }else { | 26 | }else { |
27 | - role = NewRole(&pb.Role{Uid: uid}) | 27 | + role = NewRole(uid) |
28 | role.Role.Id = 1 | 28 | role.Role.Id = 1 |
29 | role.Role.Device = "222222" | 29 | role.Role.Device = "222222" |
30 | role.Role.Level = 0 | 30 | role.Role.Level = 0 |
31 | role.Create() | 31 | role.Create() |
32 | - role.Index("uid") | 32 | + role.SetUnique("uid") |
33 | } | 33 | } |
34 | print(role) | 34 | print(role) |
35 | } | 35 | } |
36 | \ No newline at end of file | 36 | \ No newline at end of file |
@@ -0,0 +1,23 @@ | @@ -0,0 +1,23 @@ | ||
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "pro2d/components/db" | ||
5 | + "pro2d/protos/pb" | ||
6 | +) | ||
7 | + | ||
8 | +type TeamModel struct { | ||
9 | + *db.MgoColl | ||
10 | + Team *pb.Team | ||
11 | +} | ||
12 | + | ||
13 | +func NewTeam(id int64) *TeamModel{ | ||
14 | + data := &pb.Team{ | ||
15 | + Id: id, | ||
16 | + } | ||
17 | + m := &TeamModel{ | ||
18 | + MgoColl: db.NewMongoColl( "team", data, data), | ||
19 | + Team: data, | ||
20 | + } | ||
21 | + | ||
22 | + return m | ||
23 | +} | ||
0 | \ No newline at end of file | 24 | \ No newline at end of file |
utils/utils.go
1 | -package utils | ||
2 | \ No newline at end of file | 1 | \ No newline at end of file |
2 | +package utils | ||
3 | + | ||
4 | +import ( | ||
5 | + "reflect" | ||
6 | + "strings" | ||
7 | +) | ||
8 | + | ||
9 | + | ||
10 | +func FindIndex(schema interface{}) map[string]string{ | ||
11 | + s := reflect.TypeOf(schema) | ||
12 | + tb := make(map[string]string) | ||
13 | + for i := 0; i < s.NumField(); i++ { | ||
14 | + if s.Field(i).Tag.Get("index") != "" { | ||
15 | + js := strings.Split(s.Field(i).Tag.Get("json"), ",") | ||
16 | + if len(js) == 0 { | ||
17 | + continue | ||
18 | + } | ||
19 | + tb[strings.ToLower(s.Name())] = js[0] | ||
20 | + } | ||
21 | + } | ||
22 | + return tb | ||
23 | +} | ||
3 | \ No newline at end of file | 24 | \ No newline at end of file |