From 277739e2b705a94754700ea18cf558ae9d89bee3 Mon Sep 17 00:00:00 2001 From: zhaocy Date: Wed, 1 Jun 2022 15:53:16 +0800 Subject: [PATCH] =?UTF-8?q?db=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- comm/core.go | 5 +- go.mod | 4 ++ modules/login/login_comp.go | 27 ++++++++++ modules/login/module.go | 27 ++++++++++ pb/proto/user_db.proto | 12 ++--- pb/user_db.pb.go | 101 +++++++++++++++--------------------- scripts/pb.bat | 16 ------ services/worker/main.go | 5 +- sys/cache/user.go | 2 +- sys/cache/user_test.go | 19 +++++++ sys/db/user.go | 26 ++++++++++ sys/db/user_test.go | 49 +++++++++++++++++ 13 files changed, 207 insertions(+), 88 deletions(-) create mode 100644 modules/login/login_comp.go create mode 100644 modules/login/module.go delete mode 100644 scripts/pb.bat create mode 100644 sys/cache/user_test.go create mode 100644 sys/db/user_test.go diff --git a/.gitignore b/.gitignore index 9894f773a..80185ec69 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,7 @@ *.log bin/conf -.vscode +.vscode/ ./bin/conf ./bin/log ~$*.xlsx diff --git a/comm/core.go b/comm/core.go index 4de97fdd4..ddba77f04 100644 --- a/comm/core.go +++ b/comm/core.go @@ -12,8 +12,9 @@ const ( ) const ( - SM_GateModule core.M_Modules = "SM_GateModule" //gate模块 网关服务模块 - SM_WebModule core.M_Modules = "SM_WebModule" //web模块 + SM_GateModule core.M_Modules = "SM_GateModule" //gate模块 网关服务模块 + SM_WebModule core.M_Modules = "SM_WebModule" //web模块 + SM_LoginModule core.M_Modules = "SM_LoginModule" //web模块 ) const ( //Rpc diff --git a/go.mod b/go.mod index e58354893..486587b1f 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( github.com/gorilla/websocket v1.4.2 github.com/liwei1dao/lego v0.0.0-20220531091126-a21bb3766fbc + github.com/stretchr/testify v1.7.1 google.golang.org/protobuf v1.28.0 ) @@ -20,6 +21,7 @@ require ( github.com/cenkalti/backoff v2.2.1+incompatible // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/cheekybits/genny v1.0.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-jump v0.0.0-20211018200510-ba001c3ffce0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/edwingeng/doublejump v0.0.0-20210724020454-c82f1bcb3280 // indirect @@ -78,6 +80,7 @@ require ( github.com/onsi/ginkgo v1.16.5 // indirect github.com/philhofer/fwd v1.1.1 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/robfig/cron/v3 v3.0.1 // indirect github.com/rpcxio/libkv v0.5.1-0.20210420120011-1fceaedca8a5 // indirect github.com/rs/cors v1.8.2 // indirect @@ -120,6 +123,7 @@ require ( gopkg.in/ini.v1 v1.62.0 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) // replace github.com/liwei1dao/lego => F:\work\go\lego diff --git a/modules/login/login_comp.go b/modules/login/login_comp.go new file mode 100644 index 000000000..f418c7d66 --- /dev/null +++ b/modules/login/login_comp.go @@ -0,0 +1,27 @@ +package login + +import ( + "context" + "go_dreamfactory/comm" + "go_dreamfactory/modules" + "go_dreamfactory/pb" + + "github.com/liwei1dao/lego/sys/log" +) + +type LoginComp struct { + modules.MComp_GateComp +} + +//登录 +func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, rsp *pb.UserLoginReq) error { + log.Debugf("User - Login: session:%v rsp:%v", session.ToString(), rsp) + + return nil +} + +func (this *LoginComp) Logout(ctx context.Context, session comm.IUserSession, rsp *pb.UserLoginReq) error { + log.Debugf("User - Logout: session:%v rsp:%v", session.ToString(), rsp) + + return nil +} diff --git a/modules/login/module.go b/modules/login/module.go new file mode 100644 index 000000000..282d94a0f --- /dev/null +++ b/modules/login/module.go @@ -0,0 +1,27 @@ +package login + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/modules" + + "github.com/liwei1dao/lego/core" +) + +func NewModule() core.IModule { + m := new(Login) + return m +} + +type Login struct { + modules.ModuleBase + user_comp *LoginComp +} + +func (this *Login) GetType() core.M_Modules { + return comm.SM_LoginModule +} + +func (this *Login) OnInstallComp() { + this.ModuleBase.OnInstallComp() + this.user_comp = this.RegisterComp(new(LoginComp)).(*LoginComp) +} diff --git a/pb/proto/user_db.proto b/pb/proto/user_db.proto index 06156fa3d..2276b00f7 100644 --- a/pb/proto/user_db.proto +++ b/pb/proto/user_db.proto @@ -2,17 +2,15 @@ syntax = "proto3"; option go_package = ".;pb"; message Cache_UserData { - uint32 UserId = 1; string SessionId = 2; string GatewayServiceId = 3; - string NiceName = 4; - string Email = 5; - string Password = 6; + DB_UserData UserData = 4; } message DB_UserData { uint32 UserId = 1; //tags:{bson:"_id"}动态Id - string NiceName = 2; - string Email = 3; - string Password = 4; + string account = 2; + string NiceName = 3; + string Email = 4; + string Password = 5; } \ No newline at end of file diff --git a/pb/user_db.pb.go b/pb/user_db.pb.go index 9b2da8264..136214325 100644 --- a/pb/user_db.pb.go +++ b/pb/user_db.pb.go @@ -25,12 +25,9 @@ type Cache_UserData struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - UserId uint32 `protobuf:"varint,1,opt,name=UserId,proto3" json:"UserId,omitempty"` - SessionId string `protobuf:"bytes,2,opt,name=SessionId,proto3" json:"SessionId,omitempty"` - GatewayServiceId string `protobuf:"bytes,3,opt,name=GatewayServiceId,proto3" json:"GatewayServiceId,omitempty"` - NiceName string `protobuf:"bytes,4,opt,name=NiceName,proto3" json:"NiceName,omitempty"` - Email string `protobuf:"bytes,5,opt,name=Email,proto3" json:"Email,omitempty"` - Password string `protobuf:"bytes,6,opt,name=Password,proto3" json:"Password,omitempty"` + SessionId string `protobuf:"bytes,2,opt,name=SessionId,proto3" json:"SessionId,omitempty"` + GatewayServiceId string `protobuf:"bytes,3,opt,name=GatewayServiceId,proto3" json:"GatewayServiceId,omitempty"` + UserData *DB_UserData `protobuf:"bytes,4,opt,name=UserData,proto3" json:"UserData,omitempty"` } func (x *Cache_UserData) Reset() { @@ -65,13 +62,6 @@ func (*Cache_UserData) Descriptor() ([]byte, []int) { return file_user_db_proto_rawDescGZIP(), []int{0} } -func (x *Cache_UserData) GetUserId() uint32 { - if x != nil { - return x.UserId - } - return 0 -} - func (x *Cache_UserData) GetSessionId() string { if x != nil { return x.SessionId @@ -86,25 +76,11 @@ func (x *Cache_UserData) GetGatewayServiceId() string { return "" } -func (x *Cache_UserData) GetNiceName() string { +func (x *Cache_UserData) GetUserData() *DB_UserData { if x != nil { - return x.NiceName + return x.UserData } - return "" -} - -func (x *Cache_UserData) GetEmail() string { - if x != nil { - return x.Email - } - return "" -} - -func (x *Cache_UserData) GetPassword() string { - if x != nil { - return x.Password - } - return "" + return nil } type DB_UserData struct { @@ -113,9 +89,10 @@ type DB_UserData struct { unknownFields protoimpl.UnknownFields UserId uint32 `protobuf:"varint,1,opt,name=UserId,proto3" json:"UserId,omitempty" bson:"_id"` //tags:{bson:"_id"}动态Id - NiceName string `protobuf:"bytes,2,opt,name=NiceName,proto3" json:"NiceName,omitempty"` - Email string `protobuf:"bytes,3,opt,name=Email,proto3" json:"Email,omitempty"` - Password string `protobuf:"bytes,4,opt,name=Password,proto3" json:"Password,omitempty"` + Account string `protobuf:"bytes,2,opt,name=account,proto3" json:"account,omitempty"` + NiceName string `protobuf:"bytes,3,opt,name=NiceName,proto3" json:"NiceName,omitempty"` + Email string `protobuf:"bytes,4,opt,name=Email,proto3" json:"Email,omitempty"` + Password string `protobuf:"bytes,5,opt,name=Password,proto3" json:"Password,omitempty"` } func (x *DB_UserData) Reset() { @@ -157,6 +134,13 @@ func (x *DB_UserData) GetUserId() uint32 { return 0 } +func (x *DB_UserData) GetAccount() string { + if x != nil { + return x.Account + } + return "" +} + func (x *DB_UserData) GetNiceName() string { if x != nil { return x.NiceName @@ -182,27 +166,25 @@ var File_user_db_proto protoreflect.FileDescriptor var file_user_db_proto_rawDesc = []byte{ 0x0a, 0x0d, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, - 0xc0, 0x01, 0x0a, 0x0e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, - 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x0d, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x53, 0x65, - 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x53, - 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x2a, 0x0a, 0x10, 0x47, 0x61, 0x74, 0x65, - 0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x10, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, - 0x63, 0x65, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x4e, 0x69, 0x63, 0x65, 0x4e, 0x61, 0x6d, 0x65, - 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x4e, 0x69, 0x63, 0x65, 0x4e, 0x61, 0x6d, 0x65, - 0x12, 0x14, 0x0a, 0x05, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x05, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x12, 0x1a, 0x0a, 0x08, 0x50, 0x61, 0x73, 0x73, 0x77, 0x6f, - 0x72, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x50, 0x61, 0x73, 0x73, 0x77, 0x6f, - 0x72, 0x64, 0x22, 0x73, 0x0a, 0x0b, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, - 0x61, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x0d, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x4e, 0x69, 0x63, - 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x4e, 0x69, 0x63, - 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x18, 0x03, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x12, 0x1a, 0x0a, 0x08, 0x50, - 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x50, - 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, - 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x84, 0x01, 0x0a, 0x0e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, + 0x74, 0x61, 0x12, 0x1c, 0x0a, 0x09, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, + 0x12, 0x2a, 0x0a, 0x10, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, + 0x63, 0x65, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x10, 0x47, 0x61, 0x74, 0x65, + 0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, 0x64, 0x12, 0x28, 0x0a, 0x08, + 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, + 0x2e, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x52, 0x08, 0x55, 0x73, + 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x22, 0x8d, 0x01, 0x0a, 0x0b, 0x44, 0x42, 0x5f, 0x55, 0x73, + 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x18, + 0x0a, 0x07, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x07, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x4e, 0x69, 0x63, 0x65, + 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x4e, 0x69, 0x63, 0x65, + 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x18, 0x04, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x05, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x12, 0x1a, 0x0a, 0x08, 0x50, 0x61, + 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x50, 0x61, + 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -223,11 +205,12 @@ var file_user_db_proto_goTypes = []interface{}{ (*DB_UserData)(nil), // 1: DB_UserData } var file_user_db_proto_depIdxs = []int32{ - 0, // [0:0] is the sub-list for method output_type - 0, // [0:0] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name + 1, // 0: Cache_UserData.UserData:type_name -> DB_UserData + 1, // [1:1] is the sub-list for method output_type + 1, // [1:1] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name } func init() { file_user_db_proto_init() } diff --git a/scripts/pb.bat b/scripts/pb.bat deleted file mode 100644 index 5fe3c22b6..000000000 --- a/scripts/pb.bat +++ /dev/null @@ -1,16 +0,0 @@ -@echo off - -set PROJECT_ROOT=E:/projects/workspace/go_dreamfactory - -set SRC_CLIENT=%PROJECT_ROOT%\proto\client -set TAR_CLIENT=%PROJECT_ROOT%\pb\client - -set SRC_SERVER=%PROJECT_ROOT%\proto\server -set TAR_SERVER=%PROJECT_ROOT%\pb\server - -::protoc -I %SRC_CLIENT% --go_out=%TAR_CLIENT% %SRC_CLIENT%/*.proto -protoc --proto_path=%SRC_CLIENT% --go_out=%TAR_CLIENT% --go_opt=paths=source_relative %SRC_CLIENT%\*.proto - -protoc --proto_path=%SRC_SERVER% --go_out=%TAR_SERVER% --go_opt=paths=source_relative %SRC_SERVER%\*.proto - -pause \ No newline at end of file diff --git a/services/worker/main.go b/services/worker/main.go index 99a9f51f2..e6d1f74b9 100644 --- a/services/worker/main.go +++ b/services/worker/main.go @@ -2,7 +2,7 @@ package main import ( "flag" - "go_dreamfactory/modules/web" + "go_dreamfactory/modules/login" "go_dreamfactory/services" "github.com/liwei1dao/lego" @@ -24,7 +24,8 @@ func main() { services.NewGateRouteComp(), ) lego.Run(s, //运行模块 - web.NewModule(), + // web.NewModule(), + login.NewModule(), ) } diff --git a/sys/cache/user.go b/sys/cache/user.go index 8a96650fe..bb7384223 100644 --- a/sys/cache/user.go +++ b/sys/cache/user.go @@ -11,6 +11,6 @@ const ( //Redis ) func (this *Cache) UpdateUser(data *pb.Cache_UserData) (err error) { - + return } diff --git a/sys/cache/user_test.go b/sys/cache/user_test.go new file mode 100644 index 000000000..64e3874eb --- /dev/null +++ b/sys/cache/user_test.go @@ -0,0 +1,19 @@ +package cache + +import ( + "testing" + + "github.com/liwei1dao/lego/sys/redis" +) + +var rds redis.IRedis + +func TestMain(m *testing.M) { + redis.NewSys(redis.Redis_Cluster_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}), + redis.SetRedis_Cluster_Password("")) + defer m.Run() +} + +func TestSet(t *testing.T) { + +} diff --git a/sys/db/user.go b/sys/db/user.go index 72ded4736..4900a70ac 100644 --- a/sys/db/user.go +++ b/sys/db/user.go @@ -4,12 +4,38 @@ import ( "go_dreamfactory/pb" "github.com/liwei1dao/lego/core" + "go.mongodb.org/mongo-driver/bson" ) const ( //Redis DB_UserTable core.SqlTable = "user" //会话列表 ) +func (this *DB) FindUserByAccount(account string) (*pb.DB_UserData, error) { + filter := bson.D{ + {"account", account}, + } + sr := this.mgo.FindOne(DB_UserTable, filter) + user := &pb.DB_UserData{} + err := sr.Decode(user) + return user, err +} + +func (this *DB) FindUserById(id int) (*pb.DB_UserData, error) { + filter := bson.D{ + {"_id", id}, + } + sr := this.mgo.FindOne(DB_UserTable, filter) + user := &pb.DB_UserData{} + err := sr.Decode(user) + return user, err +} + +func (this *DB) CreateUser(user *pb.DB_UserData) error { + _, err := this.mgo.InsertOne(DB_UserTable, user) + return err +} + //更新用户数据到DB func (this *DB) UpdateUser(data *pb.DB_UserData) (err error) { diff --git a/sys/db/user_test.go b/sys/db/user_test.go new file mode 100644 index 000000000..c76739d2c --- /dev/null +++ b/sys/db/user_test.go @@ -0,0 +1,49 @@ +package db + +import ( + "go_dreamfactory/pb" + "log" + "os" + "testing" + + "github.com/liwei1dao/lego/sys/mgo" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var db *DB + +func TestMain(m *testing.M) { + imgo, err := mgo.NewSys(mgo.SetMongodbUrl("mongodb://admin:123456@10.0.0.9:27018"), mgo.SetMongodbDatabase("dreamfactory")) + if err != nil { + log.Fatal(err) + } + + db = &DB{ + mgo: imgo, + } + defer os.Exit(m.Run()) +} + +func TestCreate(t *testing.T) { + user := &pb.DB_UserData{ + UserId: 1, + Account: "legu1", + NiceName: "乐谷1", + Email: "1111@legu.com", + } + + err := db.CreateUser(user) + require.Nil(t, err) +} + +func TestFindOne(t *testing.T) { + user, err := db.FindUserById(1) + require.Nil(t, err) + assert.Equal(t, "legu1", user.Account) + + user2, err := db.FindUserByAccount("legu1") + require.Nil(t, err) + assert.Equal(t, "legu1", user2.Account) + +}