Commit ee23102d57f95cfa8c4f81cbf53a73d49501293c

Authored by zhangqijia
0 parents

支持mongo, grpc接服务器

.gitignore 0 → 100644
  1 +++ a/.gitignore
... ... @@ -0,0 +1,4 @@
  1 +.idea
  2 +vendor
  3 +
  4 +*.log
0 5 \ No newline at end of file
... ...
Makefile 0 → 100644
  1 +++ a/Makefile
... ... @@ -0,0 +1,15 @@
  1 +
  2 +
  3 +all: ge build run
  4 +
  5 +ge:
  6 + protoc -I./protos --go_out=./protos --go-grpc_out=./protos ./protos/*proto
  7 +
  8 +test:
  9 + go run test/client.go
  10 +run:
  11 + go run main.go
  12 +build:
  13 + go build -o bin/server main.go
  14 +
  15 +.PHONY: all build protos test
0 16 \ No newline at end of file
... ...
README.md 0 → 100644
  1 +++ a/README.md
... ... @@ -0,0 +1,34 @@
  1 +## Pro2dServer
  2 +这是pro2d项目的服务器, 使用golang搭建
  3 +
  4 +## 技术点
  5 +* grpc
  6 +* http2
  7 +* golang
  8 +* mongo
  9 +
  10 +* etcd
  11 +* grpc 热更
  12 +* proto gorm 查询
  13 +
  14 +## 环境安装
  15 +etcd
  16 +```shell
  17 +$ go get go.etcd.io/etcd/client/v3
  18 +$ go install google.golang.org/protobuf/cmd/protoc-gen-go
  19 +$ go get google.golang.org/grpc/cmd/protoc-gen-go-grpc
  20 +$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
  21 +```
  22 +## Usage
  23 +编译 & 运行
  24 +```shell
  25 +$ make run
  26 +```
  27 +测试
  28 +```shell
  29 +$ make test
  30 +```
  31 +
  32 +
  33 +
  34 +
... ...
actions/accountaction.go 0 → 100644
  1 +++ a/actions/accountaction.go
... ... @@ -0,0 +1,39 @@
  1 +package actions
  2 +
  3 +import (
  4 + "context"
  5 + "fmt"
  6 + "pro2d/conf"
  7 + "pro2d/models"
  8 + "pro2d/protos/pb"
  9 + "pro2d/utils"
  10 +)
  11 +
  12 +func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (*pb.PubRsp, error) {
  13 + ok, account := models.AccountExistByPhone(s.Database, in.Phone)
  14 + if !ok {
  15 + account.Phone = in.Phone
  16 + account.Password = utils.Md5V(in.Password)
  17 + account.Uid = conf.SnowFlack.NextVal()
  18 + account.Device = "123123"
  19 + account.Create()
  20 + }else {
  21 + return nil, fmt.Errorf("1")
  22 + }
  23 +
  24 + return &pb.PubRsp{
  25 + Code: 0,
  26 + }, nil
  27 +
  28 +}
  29 +
  30 +func (s *AccountServer) CreateTokenHandler(ctx context.Context, in *pb.AccountInfo) (*pb.CreateTokenRsp, error) {
  31 + return &pb.CreateTokenRsp{
  32 + Token: utils.CreateToken(in),
  33 + GameService: &pb.ServiceInfo{
  34 + Id: "1",
  35 + Name: conf.GlobalConf.GameConf.Name,
  36 + Address: fmt.Sprintf("%s:%d",conf.GlobalConf.GameConf.IP, conf.GlobalConf.GameConf.Port),
  37 + },
  38 + }, nil
  39 +}
... ...
actions/basic.go 0 → 100644
  1 +++ a/actions/basic.go
... ... @@ -0,0 +1,36 @@
  1 +package actions
  2 +
  3 +import (
  4 + "fmt"
  5 + "net"
  6 + "pro2d/components/db"
  7 + "pro2d/conf"
  8 + "pro2d/utils"
  9 +)
  10 +
  11 +type BasicServer struct {
  12 + Database *db.Database
  13 +}
  14 +
  15 +func NewServer() *BasicServer {
  16 + return &BasicServer{
  17 + Database: new(db.Database),
  18 + }
  19 +}
  20 +
  21 +func (b *BasicServer) Start(sConf *conf.SConf) (net.Listener, error) {
  22 + if err := b.Database.Connect(sConf.MongoConf); err !=nil {
  23 + utils.Sugar.Debugf("db error: %v", err)
  24 + return nil, err
  25 + }
  26 +
  27 + listing := fmt.Sprintf(":%d", sConf.Port)
  28 + lis, err := net.Listen("tcp", listing)
  29 + if err != nil {
  30 + return nil, err
  31 + }
  32 + return lis, err
  33 +}
  34 +
  35 +func (b *BasicServer) Close() {
  36 +}
... ...
actions/roleaction.go 0 → 100644
  1 +++ a/actions/roleaction.go
... ... @@ -0,0 +1,88 @@
  1 +package actions
  2 +
  3 +import (
  4 + "context"
  5 + "errors"
  6 + "fmt"
  7 + "google.golang.org/grpc/metadata"
  8 + "pro2d/models"
  9 + "pro2d/protos/pb"
  10 + "pro2d/utils"
  11 + "time"
  12 +)
  13 +
  14 +func (s *GameServer) HeartBeatHandler(ctx context.Context, in *pb.Token) (*pb.PubRsp, error) {
  15 + utils.Sugar.Debugf("HeartBeatHandler被调用!!!")
  16 + //获取元数据信息
  17 + md,ok := metadata.FromIncomingContext(ctx)
  18 + if !ok {
  19 + return nil,errors.New("未传输token")
  20 + }
  21 + var (
  22 + appId string
  23 + appKey string
  24 + )
  25 + if val, ok := md["appId"]; ok {
  26 + appId = val[0]
  27 + }
  28 + if val, ok := md["appKey"]; ok {
  29 + appKey = val[0]
  30 + }
  31 + //进行校验的信息是否正确
  32 + if appId != "123" || appKey != "456" {
  33 + return nil, errors.New("token传输不正确")
  34 + }
  35 +
  36 + return &pb.PubRsp{
  37 + Code: 0,
  38 + Msg: "heart beat successful",
  39 + }, nil
  40 +}
  41 +
  42 +func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.Token) (*pb.RoleRsp, error) {
  43 + account := utils.ParseToken(in.Token)
  44 + if account == nil {
  45 + return nil, fmt.Errorf("1")
  46 + }
  47 + ok, role := models.RoleExistByUid(s.Database, account.Uid)
  48 + if !ok {
  49 + role.Role = &pb.Role{
  50 + Level: 0,
  51 + LoginTime: time.Now().Unix(),
  52 + Device: account.Device,
  53 + Uid: account.Uid,
  54 + }
  55 + role.Create()
  56 +
  57 + }
  58 + return &pb.RoleRsp{
  59 + Rsp: &pb.PubRsp{
  60 + Code: 0,
  61 + Msg: "successful",
  62 + },
  63 + Role: role.Role,
  64 + }, nil
  65 +}
  66 +
  67 +func (s *GameServer) LoginHandler(ctx context.Context, in *pb.Token) (*pb.RoleRsp, error) {
  68 + account := utils.ParseToken(in.Token)
  69 + if account == nil {
  70 + return nil, fmt.Errorf("token is error")
  71 + }
  72 + ok, role := models.RoleExistByUid(s.Database, account.Uid)
  73 + if !ok {
  74 + return &pb.RoleRsp{
  75 + Rsp: &pb.PubRsp{
  76 + Code: -1,
  77 + Msg: "role not exist",
  78 + },
  79 + }, nil
  80 + }
  81 + return &pb.RoleRsp{
  82 + Rsp: &pb.PubRsp{
  83 + Code: 0,
  84 + Msg: "successful",
  85 + },
  86 + Role: role.Role,
  87 + }, nil
  88 +}
... ...
actions/server.go 0 → 100644
  1 +++ a/actions/server.go
... ... @@ -0,0 +1,95 @@
  1 +package actions
  2 +
  3 +import (
  4 + "context"
  5 + "google.golang.org/grpc"
  6 + "google.golang.org/grpc/reflection"
  7 + "pro2d/conf"
  8 + "pro2d/protos/pb"
  9 + "pro2d/utils"
  10 +)
  11 +
  12 +type AccountServer struct{
  13 + pb.UnimplementedAccountServer
  14 + *BasicServer
  15 +}
  16 +
  17 +func NewAccountServer() *AccountServer {
  18 + return &AccountServer{
  19 + BasicServer: NewServer(),
  20 + }
  21 +}
  22 +
  23 +//拦截器
  24 +func AccountServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  25 + handler grpc.UnaryHandler) (interface{}, error) {
  26 +
  27 + utils.Sugar.Debugf("gRPC method: %s, %v", info.FullMethod, req)
  28 + resp, err := handler(ctx, req)
  29 + return resp, err
  30 +}
  31 +
  32 +func (s *AccountServer)Start() error {
  33 + lis, err := s.BasicServer.Start(conf.GlobalConf.AccountConf)
  34 + if err != nil {
  35 + return err
  36 + }
  37 +
  38 + //new一个grpc
  39 + gs := grpc.NewServer(grpc.UnaryInterceptor(AccountServerInterceptor))
  40 +
  41 + pb.RegisterAccountServer(gs, s)
  42 + reflection.Register(gs) //在给定的gRPC服务器上注册服务器反射服务
  43 +
  44 + // Serve方法在lis上接受传入连接,为每个连接创建一个ServerTransport和server的goroutine。
  45 + // 该goroutine读取gRPC请求,然后调用已注册的处理程序来响应它们。
  46 + utils.Sugar.Debugf("Start AccountServer listening on %d", conf.GlobalConf.AccountConf.Port)
  47 +
  48 + return gs.Serve(lis)
  49 +}
  50 +
  51 +func (s *AccountServer)Stop() {
  52 + s.BasicServer.Close()
  53 +}
  54 +
  55 +
  56 +type GameServer struct{
  57 + pb.UnimplementedGameServer
  58 + *BasicServer
  59 +}
  60 +
  61 +func NewGameServer() *GameServer {
  62 + return &GameServer{
  63 + BasicServer: NewServer(),
  64 + }
  65 +}
  66 +//拦截器
  67 +func GameServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  68 + handler grpc.UnaryHandler) (interface{}, error) {
  69 +
  70 + utils.Sugar.Debugf("gRPC method: %s, %v", info.FullMethod, req)
  71 + resp, err := handler(ctx, req)
  72 + return resp, err
  73 +}
  74 +
  75 +func (s *GameServer)Start() error {
  76 + lis, err := s.BasicServer.Start(conf.GlobalConf.GameConf)
  77 + if err != nil {
  78 + return err
  79 + }
  80 +
  81 + //new一个grpc
  82 + gs := grpc.NewServer(grpc.UnaryInterceptor(GameServerInterceptor))
  83 +
  84 + pb.RegisterGameServer(gs, s)
  85 + reflection.Register(gs) //在给定的gRPC服务器上注册服务器反射服务
  86 +
  87 + // Serve方法在lis上接受传入连接,为每个连接创建一个ServerTransport和server的goroutine。
  88 + // 该goroutine读取gRPC请求,然后调用已注册的处理程序来响应它们。
  89 + utils.Sugar.Debugf("Start GameServer listening on %d", conf.GlobalConf.GameConf.Port)
  90 + return gs.Serve(lis)
  91 +}
  92 +
  93 +func (s *GameServer)Stop() {
  94 + s.BasicServer.Close()
  95 +}
0 96 \ No newline at end of file
... ...
bin/server 0 → 100755
No preview for this file type
components/db/mongo.go 0 → 100644
  1 +++ a/components/db/mongo.go
... ... @@ -0,0 +1,224 @@
  1 +package db
  2 +
  3 +import (
  4 + "context"
  5 + "fmt"
  6 + "go.mongodb.org/mongo-driver/bson"
  7 + "go.mongodb.org/mongo-driver/mongo"
  8 + "go.mongodb.org/mongo-driver/mongo/options"
  9 + "go.mongodb.org/mongo-driver/mongo/readpref"
  10 + "go.mongodb.org/mongo-driver/x/bsonx"
  11 + "pro2d/conf"
  12 + "strconv"
  13 + "time"
  14 +)
  15 +type Database struct {
  16 + Mongo *mongo.Database
  17 +}
  18 +
  19 +//初始化
  20 +func (db *Database)Connect(conf *conf.MongoConf) error {
  21 + var uri string
  22 + if conf.User!= "" {
  23 + uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s?w=majority", conf.User, conf.Password, conf.Host, conf.Port, conf.DBName)
  24 + }else {
  25 + uri = fmt.Sprintf("mongodb://%s:%d/%s?w=majority", conf.Host, conf.Port, conf.DBName)
  26 + }
  27 + // 设置连接超时时间
  28 + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(conf.TimeOut))
  29 + defer cancel()
  30 + // 通过传进来的uri连接相关的配置
  31 + o := options.Client().ApplyURI(uri)
  32 + // 设置最大连接数 - 默认是100 ,不设置就是最大 max 64
  33 + o.SetMaxPoolSize(uint64(conf.MaxNum))
  34 + // 发起链接
  35 + client, err := mongo.Connect(ctx, o)
  36 + if err != nil {
  37 + fmt.Println("ConnectToDB", err)
  38 + return err
  39 + }
  40 + // 判断服务是不是可用
  41 + if err = client.Ping(context.Background(), readpref.Primary()); err != nil {
  42 + fmt.Println("ConnectToDB", err)
  43 + return err
  44 + }
  45 +
  46 + db.Mongo = client.Database(conf.DBName)
  47 + return nil
  48 +}
  49 +
  50 +type MgoPool struct {
  51 + mgo *mongo.Database
  52 +
  53 + collection string
  54 +}
  55 +
  56 +func GetBsonD(key string, value interface{}) interface{} {
  57 + return bson.D{ {key, value}}
  58 +}
  59 +func GetBsonM(key string, value interface{}) interface{} {
  60 + return bson.M{key: value}
  61 +}
  62 +
  63 +func NewMongoPool(mgo *Database, collection string)*MgoPool {
  64 + return &MgoPool{
  65 + mgo: mgo.Mongo,
  66 + collection: collection,
  67 + }
  68 +}
  69 +
  70 +func (m *MgoPool)SetDatabase(databases string) {
  71 + //m.db = databases
  72 +}
  73 +func (m *MgoPool)SetCollect(coll string) {
  74 + m.collection = coll
  75 +}
  76 +
  77 +
  78 +//func (m *MgoPool)Insert(coll string, buffer interface{}) error {
  79 +// _, err := m.mgo.Collection(coll).InsertOne(context.TODO(), buffer)
  80 +// if err != nil {
  81 +// fmt.Println(err)
  82 +// }
  83 +// return err
  84 +//}
  85 +
  86 +//func (m *MgoPool)FindOne(coll string, filter interface{}) (interface{}, error){
  87 +// findOptions := options.FindOne()
  88 +// cur := m.MongoDBClient.Collection(coll).FindOne(context.TODO(), filter, findOptions)
  89 +// var role interface{}
  90 +// if err := cur.Decode(role); err != nil {
  91 +// return nil, err
  92 +// }
  93 +// return role, nil
  94 +//}
  95 +// 查询单个
  96 +func (m *MgoPool) FindOneKV(key string, value interface{}) *mongo.SingleResult {
  97 + collection := m.mgo.Collection(m.collection)
  98 + //collection.
  99 + filter := bson.D{ {key, value}}
  100 + singleResult := collection.FindOne(context.TODO(), filter)
  101 + return singleResult
  102 +}
  103 +
  104 +func (m *MgoPool) FindOne(pri interface{}) *mongo.SingleResult {
  105 + collection := m.mgo.Collection(m.collection)
  106 + //collection.
  107 + singleResult := collection.FindOne(context.TODO(), pri)
  108 + return singleResult
  109 +}
  110 +
  111 +//插入单个
  112 +func (m *MgoPool) InsertOne(value interface{}) *mongo.InsertOneResult {
  113 + collection := m.mgo.Collection(m.collection)
  114 + insertResult, err := collection.InsertOne(context.TODO(), value)
  115 + if err != nil {
  116 + fmt.Println(err)
  117 + }
  118 + return insertResult
  119 +}
  120 +
  121 +//查询集合里有多少数据
  122 +func (m *MgoPool) CollectionCount() (string, int64) {
  123 + collection := m.mgo.Collection(m.collection)
  124 + name := collection.Name()
  125 + size, _ := collection.EstimatedDocumentCount(context.TODO())
  126 + return name, size
  127 +}
  128 +
  129 +//按选项查询集合 Skip 跳过 Limit 读取数量 sort 1 ,-1 . 1 为最初时间读取 , -1 为最新时间读取
  130 +func (m *MgoPool) CollectionDocuments(Skip, Limit int64, sort int) *mongo.Cursor {
  131 + collection := m.mgo.Collection(m.collection)
  132 + SORT := bson.D{
  133 + {"_id", sort}} //filter := bson.D{ {key,value}}
  134 + filter := bson.D{
  135 + {}}
  136 + findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip)
  137 + //findOptions.SetLimit(i)
  138 + temp, _ := collection.Find(context.Background(), filter, findOptions)
  139 + return temp
  140 +}
  141 +
  142 +//获取集合创建时间和编号
  143 +func (m *MgoPool) ParsingId(result string) (time.Time, uint64) {
  144 + temp1 := result[:8]
  145 + timestamp, _ := strconv.ParseInt(temp1, 16, 64)
  146 + dateTime := time.Unix(timestamp, 0) //这是截获情报时间 时间格式 2019-04-24 09:23:39 +0800 CST
  147 + temp2 := result[18:]
  148 + count, _ := strconv.ParseUint(temp2, 16, 64) //截获情报的编号
  149 + return dateTime, count
  150 +}
  151 +
  152 +//删除文章和查询文章
  153 +func (m *MgoPool) DeleteAndFind(key string, value interface{}) (int64, *mongo.SingleResult) {
  154 + collection := m.mgo.Collection(m.collection)
  155 + filter := bson.D{
  156 + {key, value}}
  157 + singleResult := collection.FindOne(context.TODO(), filter)
  158 + DeleteResult, err := collection.DeleteOne(context.TODO(), filter, nil)
  159 + if err != nil {
  160 + fmt.Println("删除时出现错误,你删不掉的~")
  161 + }
  162 + return DeleteResult.DeletedCount, singleResult
  163 +}
  164 +
  165 +//删除文章
  166 +func (m *MgoPool) Delete(key string, value interface{}) int64 {
  167 + collection := m.mgo.Collection(m.collection)
  168 + filter := bson.D{ {key, value}}
  169 + count, err := collection.DeleteOne(context.TODO(), filter, nil)
  170 + if err != nil {
  171 + fmt.Println(err)
  172 + }
  173 + return count.DeletedCount
  174 +
  175 +}
  176 +
  177 +//删除多个
  178 +func (m *MgoPool) DeleteMany(key string, value interface{}) int64 {
  179 + collection := m.mgo.Collection(m.collection)
  180 + filter := bson.D{ {key, value}}
  181 +
  182 + count, err := collection.DeleteMany(context.TODO(), filter)
  183 + if err != nil {
  184 + fmt.Println(err)
  185 + }
  186 + return count.DeletedCount
  187 +}
  188 +
  189 +//索引
  190 +func (m *MgoPool) Index(key string){
  191 + collection := m.mgo.Collection(m.collection)
  192 + collection.Indexes().CreateOne(
  193 + context.Background(),
  194 + mongo.IndexModel{
  195 + Keys : bsonx.Doc{{key, bsonx.Int32(1)}},
  196 + Options: options.Index().SetUnique(true),
  197 + },
  198 + )
  199 +}
  200 +
  201 +//更新&保存
  202 +func (m *MgoPool) FindOneAndUpdate(filter interface{}, update interface{})*mongo.SingleResult {
  203 + //filter := bson.M{"name": "x", "array.name": "b"}
  204 + //update := bson.M{"array.$[item].detail": "test"}
  205 +
  206 + collection := m.mgo.Collection(m.collection)
  207 + res := collection.FindOneAndUpdate(context.Background(),
  208 + filter,
  209 + bson.M{"$set": update})
  210 + if res.Err() != nil {
  211 + return nil
  212 + }
  213 + return res
  214 +}
  215 +
  216 +func (m *MgoPool) UpdateOne(filter interface{}, update interface{})*mongo.UpdateResult {
  217 + collection := m.mgo.Collection(m.collection)
  218 + res, err := collection.UpdateOne(context.TODO(), filter, update)
  219 + if err != nil {
  220 + return nil
  221 + }
  222 +
  223 + return res
  224 +}
0 225 \ No newline at end of file
... ...
components/db/redis.go 0 → 100644
  1 +++ a/components/db/redis.go
... ... @@ -0,0 +1,54 @@
  1 +package db
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/garyburd/redigo/redis"
  6 + "pro2d/conf"
  7 + "pro2d/utils"
  8 + "time"
  9 +)
  10 +
  11 +type RedisPool struct {
  12 + RedisPool *redis.Pool
  13 +}
  14 +
  15 +func (rp *RedisPool)Connect(conf *conf.ServerConf) error {
  16 + rp.RedisPool = &redis.Pool{
  17 + //最大活跃连接数,0代表无限
  18 + MaxActive: 888,
  19 + MaxIdle: 20,
  20 + //闲置连接的超时时间
  21 + IdleTimeout: time.Second * 100,
  22 + //定义拨号获得连接的函数
  23 + Dial: func() (redis.Conn, error) {
  24 + option := []redis.DialOption{redis.DialDatabase(conf.RedisConf.DB)}
  25 + if conf.RedisConf.Auth != "" {
  26 + option = append(option, redis.DialPassword(conf.RedisConf.Auth))
  27 + }
  28 + return redis.Dial("tcp",conf.RedisConf.Address, option...)
  29 + },
  30 + }
  31 + return nil
  32 +}
  33 +
  34 +func (rp *RedisPool)Close() {
  35 + rp.RedisPool.Close()
  36 +}
  37 +
  38 +func (rp *RedisPool) Insert() error {
  39 + conn := rp.RedisPool.Get()
  40 + defer conn.Close()
  41 + reply, err := conn.Do("HKEYS", fmt.Sprintf("account:%s", "123123"))
  42 + if err != nil {
  43 + return err
  44 + }
  45 +
  46 + utils.Sugar.Debugf("%v", reply)
  47 + reply, err = conn.Do("HMSET", fmt.Sprintf("account:%s", "1231231"), "phone", "1231231", "passwd", "2131231")
  48 + if err != nil {
  49 + utils.Sugar.Errorf("%v", err)
  50 + return err
  51 + }
  52 + utils.Sugar.Debugf("%v", reply)
  53 + return nil
  54 +}
... ...
conf/RedisKeys.go 0 → 100644
  1 +++ a/conf/RedisKeys.go
... ... @@ -0,0 +1,2 @@
  1 +package conf
  2 +
... ...
conf/conf.go 0 → 100644
  1 +++ a/conf/conf.go
... ... @@ -0,0 +1,73 @@
  1 +package conf
  2 +
  3 +import (
  4 + "fmt"
  5 + lumberjack "gopkg.in/natefinch/lumberjack.v2"
  6 + "gopkg.in/yaml.v3"
  7 + "io/ioutil"
  8 + "pro2d/utils"
  9 +)
  10 +
  11 +type RedisConf struct {
  12 + Address string `json:"address"`
  13 + Auth string `json:"auth"`
  14 + DB int `json:"db"`
  15 +}
  16 +
  17 +type EndPoint struct {
  18 + Address string `json:"address"`
  19 +}
  20 +
  21 +type Etcd struct {
  22 + Endpoints []EndPoint `json:"endpoints"`
  23 +}
  24 +
  25 +type MongoConf struct {
  26 + User string `yaml:"user"`
  27 + Password string `yaml:"password"`
  28 + Host string `yaml:"host"`
  29 + Port int `yaml:"port"`
  30 + DBName string `yaml:"dbname"`
  31 + TimeOut int `yaml:"timeout"`
  32 + MaxNum int `yaml:"maxnum"`
  33 +}
  34 +
  35 +type SConf struct {
  36 + Name string `yaml:"name"`
  37 + IP string `yaml:"ip"`
  38 + Port int `yaml:"port"`
  39 + MongoConf *MongoConf `yaml:"mongo"`
  40 +}
  41 +
  42 +type ServerConf struct {
  43 + ID string `yaml:"id"`
  44 + Name string `yaml:"name"`
  45 + WorkerID int64 `yaml:"workerid"`
  46 + DatacenterID int64 `yaml:"datacenterid"`
  47 + AccountConf *SConf `yaml:"server_account"`
  48 + GameConf *SConf `yaml:"server_game"`
  49 + RedisConf *RedisConf `yaml:"redis"`
  50 + LogConf *lumberjack.Logger `json:"logconf"`
  51 + Etcd *Etcd `yaml:"etcd"`
  52 +}
  53 +
  54 +var GlobalConf ServerConf
  55 +var SnowFlack *utils.Snowflake
  56 +func init() {
  57 + configFile, err := ioutil.ReadFile("conf/conf.yaml")
  58 + if err != nil {
  59 + fmt.Printf("conf faild: %v", err)
  60 + return
  61 + }
  62 + //初始化配置
  63 + if err = yaml.Unmarshal(configFile, &GlobalConf); err != nil {
  64 + fmt.Printf("yaml unmarshal faild: %v", err)
  65 + return
  66 + }
  67 +
  68 + //初始化日志
  69 + utils.InitLogger(GlobalConf.LogConf)
  70 +
  71 + //初始化雪花算法
  72 + SnowFlack = utils.NewSnowflake(GlobalConf.WorkerID, GlobalConf.DatacenterID)
  73 +}
0 74 \ No newline at end of file
... ...
conf/conf.yaml 0 → 100644
  1 +++ a/conf/conf.yaml
... ... @@ -0,0 +1,39 @@
  1 +develop: true
  2 +name: "Pro2DServer"
  3 +workerid: 1
  4 +datacenterid: 1
  5 +server_account:
  6 + name: "account"
  7 + ip: "192.168.0.206"
  8 + port: 8848
  9 + mongo:
  10 + host: "192.168.0.206"
  11 + port: 27017
  12 + user: ""
  13 + password: ""
  14 + dbname: "account"
  15 + timeout: 2
  16 + maxnum: 50
  17 +server_game:
  18 + name: "game"
  19 + ip: "192.168.0.206"
  20 + port: 8849
  21 + mongo:
  22 + host: "192.168.0.206"
  23 + port: 27017
  24 + user: ""
  25 + password: ""
  26 + dbname: "game"
  27 + timeout: 2
  28 + maxnum: 50
  29 +
  30 +logconf:
  31 + filename: "./pro2d.log" # ⽇志⽂件路径
  32 + maxsize: 1024 # 1M=1024KB=1024000byte
  33 + maxbackups: 5 # 最多保留5个备份
  34 + maxage: 30 # days
  35 + compress: true # 是否压缩 disabled by default
  36 +
  37 +etcd:
  38 + endpoints:
  39 + - address: "192.168.0.206:2379"
0 40 \ No newline at end of file
... ...
doc/login.md 0 → 100644
  1 +++ a/doc/login.md
... ... @@ -0,0 +1,7 @@
  1 +
  2 +## 注册 & 登录流程
  3 +1. Account服务注册账号
  4 +2. Account服务createToken
  5 +3. Game服务登录,判断角色是否创建, 未创建角色则创建,返回角色信息
  6 +
  7 +
... ...
go.mod 0 → 100644
  1 +++ a/go.mod
... ... @@ -0,0 +1,37 @@
  1 +module pro2d
  2 +
  3 +go 1.17
  4 +
  5 +require (
  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
  9 + github.com/golang/protobuf v1.5.2
  10 + go.mongodb.org/mongo-driver v1.8.3
  11 + go.uber.org/zap v1.17.0
  12 + google.golang.org/grpc v1.38.0
  13 + google.golang.org/protobuf v1.27.1
  14 + gopkg.in/natefinch/lumberjack.v2 v2.0.0
  15 + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
  16 +)
  17 +
  18 +require (
  19 + github.com/go-stack/stack v1.8.0 // indirect
  20 + github.com/golang/snappy v0.0.1 // indirect
  21 + github.com/klauspost/compress v1.13.6 // indirect
  22 + github.com/pkg/errors v0.9.1 // indirect
  23 + github.com/xdg-go/pbkdf2 v1.0.0 // indirect
  24 + github.com/xdg-go/scram v1.0.2 // indirect
  25 + github.com/xdg-go/stringprep v1.0.2 // indirect
  26 + github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
  27 + go.uber.org/atomic v1.7.0 // indirect
  28 + go.uber.org/multierr v1.6.0 // indirect
  29 + golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f // indirect
  30 + golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
  31 + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
  32 + golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 // indirect
  33 + golang.org/x/text v0.3.5 // indirect
  34 + google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
  35 + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
  36 + gopkg.in/yaml.v2 v2.4.0 // indirect
  37 +)
... ...
go.sum 0 → 100644
  1 +++ a/go.sum
... ... @@ -0,0 +1,180 @@
  1 +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
  2 +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
  3 +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
  4 +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
  5 +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
  6 +github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
  7 +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
  8 +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
  9 +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
  10 +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
  11 +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
  12 +github.com/envoyproxy/go-control-plane v0.9.0/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=
  15 +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
  16 +github.com/garyburd/redigo v1.6.3 h1:HCeeRluvAgMusMomi1+6Y5dmFOdYV/JzoRrrbFlkGIc=
  17 +github.com/garyburd/redigo v1.6.3/go.mod h1:rTb6epsqigu3kYKBnaF028A7Tf/Aw5s0cqA47doKKqw=
  18 +github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
  19 +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
  20 +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
  21 +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
  22 +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
  23 +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
  24 +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
  25 +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
  26 +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
  27 +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
  28 +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
  29 +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
  30 +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
  31 +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
  32 +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
  33 +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
  34 +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
  35 +github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
  36 +github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
  37 +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
  38 +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
  39 +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
  40 +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
  41 +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
  42 +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
  43 +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
  44 +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
  45 +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
  46 +github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
  47 +github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
  48 +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
  49 +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
  50 +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
  51 +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
  52 +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
  53 +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
  54 +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
  55 +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
  56 +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
  57 +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
  58 +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
  59 +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
  60 +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
  61 +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
  62 +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
  63 +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
  64 +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
  65 +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
  66 +github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
  67 +github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
  68 +github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
  69 +github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
  70 +github.com/xdg-go/scram v1.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w=
  71 +github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs=
  72 +github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc=
  73 +github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
  74 +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
  75 +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
  76 +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
  77 +go.mongodb.org/mongo-driver v1.8.3 h1:TDKlTkGDKm9kkJVUOAXDK5/fkqKHJVwYQSpoRfB43R4=
  78 +go.mongodb.org/mongo-driver v1.8.3/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY=
  79 +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
  80 +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
  81 +go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
  82 +go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
  83 +go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U=
  84 +go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
  85 +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
  86 +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
  87 +golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f h1:aZp0e2vLN4MToVqnjNEYEtrEA8RH8U8FN1CU7JgqsPU=
  88 +golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
  89 +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
  90 +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
  91 +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
  92 +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
  93 +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
  94 +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
  95 +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
  96 +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
  97 +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
  98 +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
  99 +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
  100 +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
  101 +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
  102 +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
  103 +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
  104 +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
  105 +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
  106 +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
  107 +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
  108 +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
  109 +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
  110 +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
  111 +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
  112 +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
  113 +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
  114 +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
  115 +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
  116 +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
  117 +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
  118 +golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 h1:JWgyZ1qgdTaF3N3oxC+MdTV7qvEEgHo3otj+HB5CM7Q=
  119 +golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
  120 +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
  121 +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
  122 +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
  123 +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
  124 +golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
  125 +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
  126 +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
  127 +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
  128 +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
  129 +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
  130 +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
  131 +golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
  132 +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
  133 +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
  134 +golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
  135 +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
  136 +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
  137 +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
  138 +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
  139 +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
  140 +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
  141 +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
  142 +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
  143 +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
  144 +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
  145 +google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c h1:wtujag7C+4D6KMoulW9YauvK2lgdvCMS260jsqqBXr0=
  146 +google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0=
  147 +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
  148 +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
  149 +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
  150 +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
  151 +google.golang.org/grpc v1.38.0 h1:/9BgsAsa5nWe26HqOlvlgJnqBuktYOLCgjCPqsa56W0=
  152 +google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
  153 +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
  154 +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
  155 +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
  156 +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
  157 +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
  158 +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
  159 +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
  160 +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
  161 +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
  162 +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
  163 +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
  164 +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
  165 +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
  166 +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
  167 +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
  168 +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
  169 +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
  170 +gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
  171 +gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
  172 +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
  173 +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
  174 +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
  175 +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
  176 +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
  177 +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
  178 +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
  179 +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
  180 +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
... ...
main.go 0 → 100644
  1 +++ a/main.go
... ... @@ -0,0 +1,23 @@
  1 +package main
  2 +
  3 +import (
  4 + "pro2d/actions"
  5 + "pro2d/utils"
  6 +)
  7 +
  8 +func main() {
  9 + err := make(chan error)
  10 + accountS := actions.NewAccountServer()
  11 + go func() {
  12 + defer accountS.Stop()
  13 + err <- accountS.Start()
  14 + }()
  15 +
  16 + gameS := actions.NewGameServer()
  17 + go func() {
  18 + defer gameS.Stop()
  19 + err <- gameS.Start()
  20 + }()
  21 +
  22 + utils.Sugar.Errorf("server error: %v", <- err)
  23 +}
... ...
models/account.go 0 → 100644
  1 +++ a/models/account.go
... ... @@ -0,0 +1,32 @@
  1 +package models
  2 +
  3 +import (
  4 + "pro2d/components/db"
  5 + "pro2d/protos/pb"
  6 +)
  7 +
  8 +type AccountModel struct {
  9 + *ModelBaseMgo
  10 + *pb.AccountInfo
  11 +}
  12 +
  13 +func AccountExistByPhone(mgo *db.Database, phone string) (bool, *AccountModel){
  14 + m := NewAccount(mgo, phone)
  15 + if err := m.Load(); err != nil {
  16 + return false, m
  17 + }
  18 + return true, m
  19 +}
  20 +
  21 +func NewAccount(mgo *db.Database, phone string) *AccountModel {
  22 + ac := &pb.AccountInfo{
  23 + Phone: phone,
  24 + }
  25 + account := &AccountModel{
  26 + ModelBaseMgo: NewModelBaseMgo(mgo, "account", db.GetBsonM("phone", phone), ac),
  27 + AccountInfo: ac,
  28 + }
  29 +
  30 +
  31 + return account
  32 +}
0 33 \ No newline at end of file
... ...
models/basic.go 0 → 100644
  1 +++ a/models/basic.go
... ... @@ -0,0 +1,46 @@
  1 +package models
  2 +
  3 +import (
  4 + "pro2d/components/db"
  5 +)
  6 +
  7 +type ModelBaseMgo struct {
  8 + MonGo *db.MgoPool
  9 +
  10 + pri interface{}
  11 + schema interface{}
  12 +}
  13 +
  14 +
  15 +func NewModelBaseMgo(mgo *db.Database, collection string, pri interface{}, schema interface{}) *ModelBaseMgo{
  16 + return &ModelBaseMgo{
  17 + MonGo: db.NewMongoPool(mgo, collection),
  18 + pri: pri,
  19 + schema: schema,
  20 + }
  21 +}
  22 +
  23 +func (base *ModelBaseMgo) Load() error{
  24 + r := base.MonGo.FindOne(base.pri)
  25 + err := r.Decode(base.schema)
  26 + if err != nil {
  27 + return err
  28 + }
  29 + return nil
  30 +}
  31 +
  32 +func (base *ModelBaseMgo) Create() {
  33 + base.MonGo.InsertOne(base.schema)
  34 +}
  35 +
  36 +func (base *ModelBaseMgo) Index(key string) {
  37 + base.MonGo.Index(key)
  38 +}
  39 +
  40 +func (base *ModelBaseMgo) Update(update interface{}) {
  41 + base.MonGo.FindOneAndUpdate(base.pri, update)
  42 +}
  43 +
  44 +func (base *ModelBaseMgo) Save() {
  45 + base.MonGo.FindOneAndUpdate(base.pri, base.schema)
  46 +}
0 47 \ No newline at end of file
... ...
models/role.go 0 → 100644
  1 +++ a/models/role.go
... ... @@ -0,0 +1,38 @@
  1 +package models
  2 +
  3 +import (
  4 + "pro2d/components/db"
  5 + "pro2d/protos/pb"
  6 +)
  7 +
  8 +type RoleModel struct {
  9 + *ModelBaseMgo
  10 + *pb.Role
  11 +}
  12 +
  13 +//创建数据
  14 +//数据加载
  15 +//数据保存
  16 +
  17 +func RoleExistByUid(mgo *db.Database, uid int64) (bool, *RoleModel){
  18 + m := NewRole(mgo, uid)
  19 + if err := m.Load(); err != nil {
  20 + return false, m
  21 + }
  22 + return true, m
  23 +}
  24 +
  25 +func NewRole(mgo *db.Database, uid int64) *RoleModel {
  26 + r := &pb.Role{
  27 + Uid: uid,
  28 + }
  29 + m := &RoleModel{
  30 + ModelBaseMgo: NewModelBaseMgo(mgo, "role", db.GetBsonM("uid", uid), r),
  31 + Role: r,
  32 + }
  33 + m.Load()
  34 + return m
  35 +}
  36 +
  37 +func (m *RoleModel) LoadAll() {
  38 +}
... ...
models/role_test.go 0 → 100644
  1 +++ a/models/role_test.go
... ... @@ -0,0 +1,35 @@
  1 +package models
  2 +
  3 +import (
  4 + "pro2d/components/db"
  5 + "pro2d/conf"
  6 + "pro2d/utils"
  7 + "testing"
  8 + "time"
  9 +)
  10 +
  11 +func TestNewRole(t *testing.T) {
  12 + db := &db.Database{}
  13 + if err := db.Connect(conf.GlobalConf.GameConf.MongoConf); err != nil {
  14 + utils.Sugar.Errorf("%v", err)
  15 + return
  16 + }
  17 +
  18 +
  19 + //db.Mongo.Drop(context.Background())
  20 + var uid int64 = 1
  21 + var role *RoleModel
  22 + if ok, role := RoleExistByUid(db, uid); ok {
  23 + role.Role.Device = "111111"
  24 + role.Role.LoginTime = time.Now().Unix()
  25 + role.Save()
  26 + }else {
  27 + role = NewRole(db, uid)
  28 + role.Role.Id = "1"
  29 + role.Role.Device = "222222"
  30 + role.Role.Level = 0
  31 + role.Create()
  32 + role.Index("uid")
  33 + }
  34 + print(role)
  35 +}
0 36 \ No newline at end of file
... ...
protos/account.proto 0 → 100644
  1 +++ a/protos/account.proto
... ... @@ -0,0 +1,34 @@
  1 +syntax = "proto3";
  2 +option go_package = "./pb;pb";
  3 +
  4 +package account;
  5 +import "public.proto";
  6 +
  7 +message ServiceInfo {
  8 + string id = 1;
  9 + string name = 2;
  10 + string address = 3;
  11 +}
  12 +
  13 +message AccountInfo{
  14 + string phone = 2;
  15 + string password = 3;
  16 + int64 uid = 4;
  17 + string device = 5;
  18 +}
  19 +
  20 +message CreateTokenRsp {
  21 + string token = 1;
  22 + ServiceInfo game_service = 2;
  23 +}
  24 +
  25 +message Register {
  26 + string phone = 1;
  27 + string password = 2;
  28 + int32 code = 3;
  29 +}
  30 +
  31 +service Account{
  32 + rpc RegisterHandler(Register) returns (public.PubRsp) {}
  33 + rpc CreateTokenHandler(AccountInfo) returns (CreateTokenRsp) {}
  34 +}
0 35 \ No newline at end of file
... ...
protos/game.proto 0 → 100644
  1 +++ a/protos/game.proto
... ... @@ -0,0 +1,28 @@
  1 +syntax = "proto3";
  2 +option go_package = "./pb;pb";
  3 +
  4 +package game;
  5 +import "public.proto";
  6 +
  7 +message Role{
  8 + string id = 1;
  9 + int32 level = 3;
  10 + int64 login_time = 4;
  11 + string device = 5;
  12 + int64 uid = 6;
  13 +}
  14 +
  15 +message Token {
  16 + string token = 1;
  17 +}
  18 +
  19 +message RoleRsp {
  20 + public.PubRsp rsp = 1;
  21 + Role role = 2;
  22 +}
  23 +
  24 +service Game{
  25 + rpc HeartBeatHandler(Token) returns (public.PubRsp) {}
  26 + rpc LoginHandler(Token) returns (RoleRsp) {}
  27 + rpc CreateRoleHandler(Token) returns (RoleRsp) {}
  28 +}
0 29 \ No newline at end of file
... ...
protos/generate.sh 0 → 100755
  1 +++ a/protos/generate.sh
... ... @@ -0,0 +1,3 @@
  1 +#!/bin/bash
  2 +protoc -I. --go_out=. --go-grpc_out=. ./*proto
  3 +#./account/*.proto ./game/*.proto ./public/*.proto
0 4 \ No newline at end of file
... ...
protos/pb/account.pb.go 0 → 100644
  1 +++ a/protos/pb/account.pb.go
... ... @@ -0,0 +1,422 @@
  1 +// Code generated by protoc-gen-go. DO NOT EDIT.
  2 +// versions:
  3 +// protoc-gen-go v1.26.0
  4 +// protoc v3.17.3
  5 +// source: account.proto
  6 +
  7 +package pb
  8 +
  9 +import (
  10 + protoreflect "google.golang.org/protobuf/reflect/protoreflect"
  11 + protoimpl "google.golang.org/protobuf/runtime/protoimpl"
  12 + reflect "reflect"
  13 + sync "sync"
  14 +)
  15 +
  16 +const (
  17 + // Verify that this generated code is sufficiently up-to-date.
  18 + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
  19 + // Verify that runtime/protoimpl is sufficiently up-to-date.
  20 + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
  21 +)
  22 +
  23 +type ServiceInfo struct {
  24 + state protoimpl.MessageState
  25 + sizeCache protoimpl.SizeCache
  26 + unknownFields protoimpl.UnknownFields
  27 +
  28 + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
  29 + Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
  30 + Address string `protobuf:"bytes,3,opt,name=address,proto3" json:"address,omitempty"`
  31 +}
  32 +
  33 +func (x *ServiceInfo) Reset() {
  34 + *x = ServiceInfo{}
  35 + if protoimpl.UnsafeEnabled {
  36 + mi := &file_account_proto_msgTypes[0]
  37 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  38 + ms.StoreMessageInfo(mi)
  39 + }
  40 +}
  41 +
  42 +func (x *ServiceInfo) String() string {
  43 + return protoimpl.X.MessageStringOf(x)
  44 +}
  45 +
  46 +func (*ServiceInfo) ProtoMessage() {}
  47 +
  48 +func (x *ServiceInfo) ProtoReflect() protoreflect.Message {
  49 + mi := &file_account_proto_msgTypes[0]
  50 + if protoimpl.UnsafeEnabled && x != nil {
  51 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  52 + if ms.LoadMessageInfo() == nil {
  53 + ms.StoreMessageInfo(mi)
  54 + }
  55 + return ms
  56 + }
  57 + return mi.MessageOf(x)
  58 +}
  59 +
  60 +// Deprecated: Use ServiceInfo.ProtoReflect.Descriptor instead.
  61 +func (*ServiceInfo) Descriptor() ([]byte, []int) {
  62 + return file_account_proto_rawDescGZIP(), []int{0}
  63 +}
  64 +
  65 +func (x *ServiceInfo) GetId() string {
  66 + if x != nil {
  67 + return x.Id
  68 + }
  69 + return ""
  70 +}
  71 +
  72 +func (x *ServiceInfo) GetName() string {
  73 + if x != nil {
  74 + return x.Name
  75 + }
  76 + return ""
  77 +}
  78 +
  79 +func (x *ServiceInfo) GetAddress() string {
  80 + if x != nil {
  81 + return x.Address
  82 + }
  83 + return ""
  84 +}
  85 +
  86 +type AccountInfo struct {
  87 + state protoimpl.MessageState
  88 + sizeCache protoimpl.SizeCache
  89 + unknownFields protoimpl.UnknownFields
  90 +
  91 + Phone string `protobuf:"bytes,2,opt,name=phone,proto3" json:"phone,omitempty"`
  92 + Password string `protobuf:"bytes,3,opt,name=password,proto3" json:"password,omitempty"`
  93 + Uid int64 `protobuf:"varint,4,opt,name=uid,proto3" json:"uid,omitempty"`
  94 + Device string `protobuf:"bytes,5,opt,name=device,proto3" json:"device,omitempty"`
  95 +}
  96 +
  97 +func (x *AccountInfo) Reset() {
  98 + *x = AccountInfo{}
  99 + if protoimpl.UnsafeEnabled {
  100 + mi := &file_account_proto_msgTypes[1]
  101 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  102 + ms.StoreMessageInfo(mi)
  103 + }
  104 +}
  105 +
  106 +func (x *AccountInfo) String() string {
  107 + return protoimpl.X.MessageStringOf(x)
  108 +}
  109 +
  110 +func (*AccountInfo) ProtoMessage() {}
  111 +
  112 +func (x *AccountInfo) ProtoReflect() protoreflect.Message {
  113 + mi := &file_account_proto_msgTypes[1]
  114 + if protoimpl.UnsafeEnabled && x != nil {
  115 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  116 + if ms.LoadMessageInfo() == nil {
  117 + ms.StoreMessageInfo(mi)
  118 + }
  119 + return ms
  120 + }
  121 + return mi.MessageOf(x)
  122 +}
  123 +
  124 +// Deprecated: Use AccountInfo.ProtoReflect.Descriptor instead.
  125 +func (*AccountInfo) Descriptor() ([]byte, []int) {
  126 + return file_account_proto_rawDescGZIP(), []int{1}
  127 +}
  128 +
  129 +func (x *AccountInfo) GetPhone() string {
  130 + if x != nil {
  131 + return x.Phone
  132 + }
  133 + return ""
  134 +}
  135 +
  136 +func (x *AccountInfo) GetPassword() string {
  137 + if x != nil {
  138 + return x.Password
  139 + }
  140 + return ""
  141 +}
  142 +
  143 +func (x *AccountInfo) GetUid() int64 {
  144 + if x != nil {
  145 + return x.Uid
  146 + }
  147 + return 0
  148 +}
  149 +
  150 +func (x *AccountInfo) GetDevice() string {
  151 + if x != nil {
  152 + return x.Device
  153 + }
  154 + return ""
  155 +}
  156 +
  157 +type CreateTokenRsp struct {
  158 + state protoimpl.MessageState
  159 + sizeCache protoimpl.SizeCache
  160 + unknownFields protoimpl.UnknownFields
  161 +
  162 + Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
  163 + GameService *ServiceInfo `protobuf:"bytes,2,opt,name=game_service,json=gameService,proto3" json:"game_service,omitempty"`
  164 +}
  165 +
  166 +func (x *CreateTokenRsp) Reset() {
  167 + *x = CreateTokenRsp{}
  168 + if protoimpl.UnsafeEnabled {
  169 + mi := &file_account_proto_msgTypes[2]
  170 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  171 + ms.StoreMessageInfo(mi)
  172 + }
  173 +}
  174 +
  175 +func (x *CreateTokenRsp) String() string {
  176 + return protoimpl.X.MessageStringOf(x)
  177 +}
  178 +
  179 +func (*CreateTokenRsp) ProtoMessage() {}
  180 +
  181 +func (x *CreateTokenRsp) ProtoReflect() protoreflect.Message {
  182 + mi := &file_account_proto_msgTypes[2]
  183 + if protoimpl.UnsafeEnabled && x != nil {
  184 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  185 + if ms.LoadMessageInfo() == nil {
  186 + ms.StoreMessageInfo(mi)
  187 + }
  188 + return ms
  189 + }
  190 + return mi.MessageOf(x)
  191 +}
  192 +
  193 +// Deprecated: Use CreateTokenRsp.ProtoReflect.Descriptor instead.
  194 +func (*CreateTokenRsp) Descriptor() ([]byte, []int) {
  195 + return file_account_proto_rawDescGZIP(), []int{2}
  196 +}
  197 +
  198 +func (x *CreateTokenRsp) GetToken() string {
  199 + if x != nil {
  200 + return x.Token
  201 + }
  202 + return ""
  203 +}
  204 +
  205 +func (x *CreateTokenRsp) GetGameService() *ServiceInfo {
  206 + if x != nil {
  207 + return x.GameService
  208 + }
  209 + return nil
  210 +}
  211 +
  212 +type Register struct {
  213 + state protoimpl.MessageState
  214 + sizeCache protoimpl.SizeCache
  215 + unknownFields protoimpl.UnknownFields
  216 +
  217 + Phone string `protobuf:"bytes,1,opt,name=phone,proto3" json:"phone,omitempty"`
  218 + Password string `protobuf:"bytes,2,opt,name=password,proto3" json:"password,omitempty"`
  219 + Code int32 `protobuf:"varint,3,opt,name=code,proto3" json:"code,omitempty"`
  220 +}
  221 +
  222 +func (x *Register) Reset() {
  223 + *x = Register{}
  224 + if protoimpl.UnsafeEnabled {
  225 + mi := &file_account_proto_msgTypes[3]
  226 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  227 + ms.StoreMessageInfo(mi)
  228 + }
  229 +}
  230 +
  231 +func (x *Register) String() string {
  232 + return protoimpl.X.MessageStringOf(x)
  233 +}
  234 +
  235 +func (*Register) ProtoMessage() {}
  236 +
  237 +func (x *Register) ProtoReflect() protoreflect.Message {
  238 + mi := &file_account_proto_msgTypes[3]
  239 + if protoimpl.UnsafeEnabled && x != nil {
  240 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  241 + if ms.LoadMessageInfo() == nil {
  242 + ms.StoreMessageInfo(mi)
  243 + }
  244 + return ms
  245 + }
  246 + return mi.MessageOf(x)
  247 +}
  248 +
  249 +// Deprecated: Use Register.ProtoReflect.Descriptor instead.
  250 +func (*Register) Descriptor() ([]byte, []int) {
  251 + return file_account_proto_rawDescGZIP(), []int{3}
  252 +}
  253 +
  254 +func (x *Register) GetPhone() string {
  255 + if x != nil {
  256 + return x.Phone
  257 + }
  258 + return ""
  259 +}
  260 +
  261 +func (x *Register) GetPassword() string {
  262 + if x != nil {
  263 + return x.Password
  264 + }
  265 + return ""
  266 +}
  267 +
  268 +func (x *Register) GetCode() int32 {
  269 + if x != nil {
  270 + return x.Code
  271 + }
  272 + return 0
  273 +}
  274 +
  275 +var File_account_proto protoreflect.FileDescriptor
  276 +
  277 +var file_account_proto_rawDesc = []byte{
  278 + 0x0a, 0x0d, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12,
  279 + 0x07, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x1a, 0x0c, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63,
  280 + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x4b, 0x0a, 0x0b, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63,
  281 + 0x65, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
  282 + 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20,
  283 + 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x64, 0x64,
  284 + 0x72, 0x65, 0x73, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x64, 0x64, 0x72,
  285 + 0x65, 0x73, 0x73, 0x22, 0x69, 0x0a, 0x0b, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x6e,
  286 + 0x66, 0x6f, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28,
  287 + 0x09, 0x52, 0x05, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73,
  288 + 0x77, 0x6f, 0x72, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73,
  289 + 0x77, 0x6f, 0x72, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28,
  290 + 0x03, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65,
  291 + 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x22, 0x5f,
  292 + 0x0a, 0x0e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x73, 0x70,
  293 + 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
  294 + 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x37, 0x0a, 0x0c, 0x67, 0x61, 0x6d, 0x65, 0x5f, 0x73,
  295 + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x61,
  296 + 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, 0x6e,
  297 + 0x66, 0x6f, 0x52, 0x0b, 0x67, 0x61, 0x6d, 0x65, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22,
  298 + 0x50, 0x0a, 0x08, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x12, 0x14, 0x0a, 0x05, 0x70,
  299 + 0x68, 0x6f, 0x6e, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x70, 0x68, 0x6f, 0x6e,
  300 + 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20,
  301 + 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x12, 0x12, 0x0a,
  302 + 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64,
  303 + 0x65, 0x32, 0x88, 0x01, 0x0a, 0x07, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x36, 0x0a,
  304 + 0x0f, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72,
  305 + 0x12, 0x11, 0x2e, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73,
  306 + 0x74, 0x65, 0x72, 0x1a, 0x0e, 0x2e, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x2e, 0x50, 0x75, 0x62,
  307 + 0x52, 0x73, 0x70, 0x22, 0x00, 0x12, 0x45, 0x0a, 0x12, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x54,
  308 + 0x6f, 0x6b, 0x65, 0x6e, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x12, 0x14, 0x2e, 0x61, 0x63,
  309 + 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2e, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x6e, 0x66,
  310 + 0x6f, 0x1a, 0x17, 0x2e, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2e, 0x43, 0x72, 0x65, 0x61,
  311 + 0x74, 0x65, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x73, 0x70, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07,
  312 + 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
  313 +}
  314 +
  315 +var (
  316 + file_account_proto_rawDescOnce sync.Once
  317 + file_account_proto_rawDescData = file_account_proto_rawDesc
  318 +)
  319 +
  320 +func file_account_proto_rawDescGZIP() []byte {
  321 + file_account_proto_rawDescOnce.Do(func() {
  322 + file_account_proto_rawDescData = protoimpl.X.CompressGZIP(file_account_proto_rawDescData)
  323 + })
  324 + return file_account_proto_rawDescData
  325 +}
  326 +
  327 +var file_account_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
  328 +var file_account_proto_goTypes = []interface{}{
  329 + (*ServiceInfo)(nil), // 0: account.ServiceInfo
  330 + (*AccountInfo)(nil), // 1: account.AccountInfo
  331 + (*CreateTokenRsp)(nil), // 2: account.CreateTokenRsp
  332 + (*Register)(nil), // 3: account.Register
  333 + (*PubRsp)(nil), // 4: public.PubRsp
  334 +}
  335 +var file_account_proto_depIdxs = []int32{
  336 + 0, // 0: account.CreateTokenRsp.game_service:type_name -> account.ServiceInfo
  337 + 3, // 1: account.Account.RegisterHandler:input_type -> account.Register
  338 + 1, // 2: account.Account.CreateTokenHandler:input_type -> account.AccountInfo
  339 + 4, // 3: account.Account.RegisterHandler:output_type -> public.PubRsp
  340 + 2, // 4: account.Account.CreateTokenHandler:output_type -> account.CreateTokenRsp
  341 + 3, // [3:5] is the sub-list for method output_type
  342 + 1, // [1:3] is the sub-list for method input_type
  343 + 1, // [1:1] is the sub-list for extension type_name
  344 + 1, // [1:1] is the sub-list for extension extendee
  345 + 0, // [0:1] is the sub-list for field type_name
  346 +}
  347 +
  348 +func init() { file_account_proto_init() }
  349 +func file_account_proto_init() {
  350 + if File_account_proto != nil {
  351 + return
  352 + }
  353 + file_public_proto_init()
  354 + if !protoimpl.UnsafeEnabled {
  355 + file_account_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
  356 + switch v := v.(*ServiceInfo); i {
  357 + case 0:
  358 + return &v.state
  359 + case 1:
  360 + return &v.sizeCache
  361 + case 2:
  362 + return &v.unknownFields
  363 + default:
  364 + return nil
  365 + }
  366 + }
  367 + file_account_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
  368 + switch v := v.(*AccountInfo); i {
  369 + case 0:
  370 + return &v.state
  371 + case 1:
  372 + return &v.sizeCache
  373 + case 2:
  374 + return &v.unknownFields
  375 + default:
  376 + return nil
  377 + }
  378 + }
  379 + file_account_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
  380 + switch v := v.(*CreateTokenRsp); i {
  381 + case 0:
  382 + return &v.state
  383 + case 1:
  384 + return &v.sizeCache
  385 + case 2:
  386 + return &v.unknownFields
  387 + default:
  388 + return nil
  389 + }
  390 + }
  391 + file_account_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
  392 + switch v := v.(*Register); i {
  393 + case 0:
  394 + return &v.state
  395 + case 1:
  396 + return &v.sizeCache
  397 + case 2:
  398 + return &v.unknownFields
  399 + default:
  400 + return nil
  401 + }
  402 + }
  403 + }
  404 + type x struct{}
  405 + out := protoimpl.TypeBuilder{
  406 + File: protoimpl.DescBuilder{
  407 + GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
  408 + RawDescriptor: file_account_proto_rawDesc,
  409 + NumEnums: 0,
  410 + NumMessages: 4,
  411 + NumExtensions: 0,
  412 + NumServices: 1,
  413 + },
  414 + GoTypes: file_account_proto_goTypes,
  415 + DependencyIndexes: file_account_proto_depIdxs,
  416 + MessageInfos: file_account_proto_msgTypes,
  417 + }.Build()
  418 + File_account_proto = out.File
  419 + file_account_proto_rawDesc = nil
  420 + file_account_proto_goTypes = nil
  421 + file_account_proto_depIdxs = nil
  422 +}
... ...
protos/pb/account_grpc.pb.go 0 → 100644
  1 +++ a/protos/pb/account_grpc.pb.go
... ... @@ -0,0 +1,141 @@
  1 +// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
  2 +// versions:
  3 +// - protoc-gen-go-grpc v1.2.0
  4 +// - protoc v3.17.3
  5 +// source: account.proto
  6 +
  7 +package pb
  8 +
  9 +import (
  10 + context "context"
  11 + grpc "google.golang.org/grpc"
  12 + codes "google.golang.org/grpc/codes"
  13 + status "google.golang.org/grpc/status"
  14 +)
  15 +
  16 +// This is a compile-time assertion to ensure that this generated file
  17 +// is compatible with the grpc package it is being compiled against.
  18 +// Requires gRPC-Go v1.32.0 or later.
  19 +const _ = grpc.SupportPackageIsVersion7
  20 +
  21 +// AccountClient is the client API for Account service.
  22 +//
  23 +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
  24 +type AccountClient interface {
  25 + RegisterHandler(ctx context.Context, in *Register, opts ...grpc.CallOption) (*PubRsp, error)
  26 + CreateTokenHandler(ctx context.Context, in *AccountInfo, opts ...grpc.CallOption) (*CreateTokenRsp, error)
  27 +}
  28 +
  29 +type accountClient struct {
  30 + cc grpc.ClientConnInterface
  31 +}
  32 +
  33 +func NewAccountClient(cc grpc.ClientConnInterface) AccountClient {
  34 + return &accountClient{cc}
  35 +}
  36 +
  37 +func (c *accountClient) RegisterHandler(ctx context.Context, in *Register, opts ...grpc.CallOption) (*PubRsp, error) {
  38 + out := new(PubRsp)
  39 + err := c.cc.Invoke(ctx, "/account.Account/RegisterHandler", in, out, opts...)
  40 + if err != nil {
  41 + return nil, err
  42 + }
  43 + return out, nil
  44 +}
  45 +
  46 +func (c *accountClient) CreateTokenHandler(ctx context.Context, in *AccountInfo, opts ...grpc.CallOption) (*CreateTokenRsp, error) {
  47 + out := new(CreateTokenRsp)
  48 + err := c.cc.Invoke(ctx, "/account.Account/CreateTokenHandler", in, out, opts...)
  49 + if err != nil {
  50 + return nil, err
  51 + }
  52 + return out, nil
  53 +}
  54 +
  55 +// AccountServer is the server API for Account service.
  56 +// All implementations must embed UnimplementedAccountServer
  57 +// for forward compatibility
  58 +type AccountServer interface {
  59 + RegisterHandler(context.Context, *Register) (*PubRsp, error)
  60 + CreateTokenHandler(context.Context, *AccountInfo) (*CreateTokenRsp, error)
  61 + mustEmbedUnimplementedAccountServer()
  62 +}
  63 +
  64 +// UnimplementedAccountServer must be embedded to have forward compatible implementations.
  65 +type UnimplementedAccountServer struct {
  66 +}
  67 +
  68 +func (UnimplementedAccountServer) RegisterHandler(context.Context, *Register) (*PubRsp, error) {
  69 + return nil, status.Errorf(codes.Unimplemented, "method RegisterHandler not implemented")
  70 +}
  71 +func (UnimplementedAccountServer) CreateTokenHandler(context.Context, *AccountInfo) (*CreateTokenRsp, error) {
  72 + return nil, status.Errorf(codes.Unimplemented, "method CreateTokenHandler not implemented")
  73 +}
  74 +func (UnimplementedAccountServer) mustEmbedUnimplementedAccountServer() {}
  75 +
  76 +// UnsafeAccountServer may be embedded to opt out of forward compatibility for this service.
  77 +// Use of this interface is not recommended, as added methods to AccountServer will
  78 +// result in compilation errors.
  79 +type UnsafeAccountServer interface {
  80 + mustEmbedUnimplementedAccountServer()
  81 +}
  82 +
  83 +func RegisterAccountServer(s grpc.ServiceRegistrar, srv AccountServer) {
  84 + s.RegisterService(&Account_ServiceDesc, srv)
  85 +}
  86 +
  87 +func _Account_RegisterHandler_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
  88 + in := new(Register)
  89 + if err := dec(in); err != nil {
  90 + return nil, err
  91 + }
  92 + if interceptor == nil {
  93 + return srv.(AccountServer).RegisterHandler(ctx, in)
  94 + }
  95 + info := &grpc.UnaryServerInfo{
  96 + Server: srv,
  97 + FullMethod: "/account.Account/RegisterHandler",
  98 + }
  99 + handler := func(ctx context.Context, req interface{}) (interface{}, error) {
  100 + return srv.(AccountServer).RegisterHandler(ctx, req.(*Register))
  101 + }
  102 + return interceptor(ctx, in, info, handler)
  103 +}
  104 +
  105 +func _Account_CreateTokenHandler_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
  106 + in := new(AccountInfo)
  107 + if err := dec(in); err != nil {
  108 + return nil, err
  109 + }
  110 + if interceptor == nil {
  111 + return srv.(AccountServer).CreateTokenHandler(ctx, in)
  112 + }
  113 + info := &grpc.UnaryServerInfo{
  114 + Server: srv,
  115 + FullMethod: "/account.Account/CreateTokenHandler",
  116 + }
  117 + handler := func(ctx context.Context, req interface{}) (interface{}, error) {
  118 + return srv.(AccountServer).CreateTokenHandler(ctx, req.(*AccountInfo))
  119 + }
  120 + return interceptor(ctx, in, info, handler)
  121 +}
  122 +
  123 +// Account_ServiceDesc is the grpc.ServiceDesc for Account service.
  124 +// It's only intended for direct use with grpc.RegisterService,
  125 +// and not to be introspected or modified (even as a copy)
  126 +var Account_ServiceDesc = grpc.ServiceDesc{
  127 + ServiceName: "account.Account",
  128 + HandlerType: (*AccountServer)(nil),
  129 + Methods: []grpc.MethodDesc{
  130 + {
  131 + MethodName: "RegisterHandler",
  132 + Handler: _Account_RegisterHandler_Handler,
  133 + },
  134 + {
  135 + MethodName: "CreateTokenHandler",
  136 + Handler: _Account_CreateTokenHandler_Handler,
  137 + },
  138 + },
  139 + Streams: []grpc.StreamDesc{},
  140 + Metadata: "account.proto",
  141 +}
... ...
protos/pb/game.pb.go 0 → 100644
  1 +++ a/protos/pb/game.pb.go
... ... @@ -0,0 +1,334 @@
  1 +// Code generated by protoc-gen-go. DO NOT EDIT.
  2 +// versions:
  3 +// protoc-gen-go v1.26.0
  4 +// protoc v3.17.3
  5 +// source: game.proto
  6 +
  7 +package pb
  8 +
  9 +import (
  10 + protoreflect "google.golang.org/protobuf/reflect/protoreflect"
  11 + protoimpl "google.golang.org/protobuf/runtime/protoimpl"
  12 + reflect "reflect"
  13 + sync "sync"
  14 +)
  15 +
  16 +const (
  17 + // Verify that this generated code is sufficiently up-to-date.
  18 + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
  19 + // Verify that runtime/protoimpl is sufficiently up-to-date.
  20 + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
  21 +)
  22 +
  23 +type Role struct {
  24 + state protoimpl.MessageState
  25 + sizeCache protoimpl.SizeCache
  26 + unknownFields protoimpl.UnknownFields
  27 +
  28 + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
  29 + Level int32 `protobuf:"varint,3,opt,name=level,proto3" json:"level,omitempty"`
  30 + LoginTime int64 `protobuf:"varint,4,opt,name=login_time,json=loginTime,proto3" json:"login_time,omitempty"`
  31 + Device string `protobuf:"bytes,5,opt,name=device,proto3" json:"device,omitempty"`
  32 + Uid int64 `protobuf:"varint,6,opt,name=uid,proto3" json:"uid,omitempty"`
  33 +}
  34 +
  35 +func (x *Role) Reset() {
  36 + *x = Role{}
  37 + if protoimpl.UnsafeEnabled {
  38 + mi := &file_game_proto_msgTypes[0]
  39 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  40 + ms.StoreMessageInfo(mi)
  41 + }
  42 +}
  43 +
  44 +func (x *Role) String() string {
  45 + return protoimpl.X.MessageStringOf(x)
  46 +}
  47 +
  48 +func (*Role) ProtoMessage() {}
  49 +
  50 +func (x *Role) ProtoReflect() protoreflect.Message {
  51 + mi := &file_game_proto_msgTypes[0]
  52 + if protoimpl.UnsafeEnabled && x != nil {
  53 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  54 + if ms.LoadMessageInfo() == nil {
  55 + ms.StoreMessageInfo(mi)
  56 + }
  57 + return ms
  58 + }
  59 + return mi.MessageOf(x)
  60 +}
  61 +
  62 +// Deprecated: Use Role.ProtoReflect.Descriptor instead.
  63 +func (*Role) Descriptor() ([]byte, []int) {
  64 + return file_game_proto_rawDescGZIP(), []int{0}
  65 +}
  66 +
  67 +func (x *Role) GetId() string {
  68 + if x != nil {
  69 + return x.Id
  70 + }
  71 + return ""
  72 +}
  73 +
  74 +func (x *Role) GetLevel() int32 {
  75 + if x != nil {
  76 + return x.Level
  77 + }
  78 + return 0
  79 +}
  80 +
  81 +func (x *Role) GetLoginTime() int64 {
  82 + if x != nil {
  83 + return x.LoginTime
  84 + }
  85 + return 0
  86 +}
  87 +
  88 +func (x *Role) GetDevice() string {
  89 + if x != nil {
  90 + return x.Device
  91 + }
  92 + return ""
  93 +}
  94 +
  95 +func (x *Role) GetUid() int64 {
  96 + if x != nil {
  97 + return x.Uid
  98 + }
  99 + return 0
  100 +}
  101 +
  102 +type Token struct {
  103 + state protoimpl.MessageState
  104 + sizeCache protoimpl.SizeCache
  105 + unknownFields protoimpl.UnknownFields
  106 +
  107 + Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
  108 +}
  109 +
  110 +func (x *Token) Reset() {
  111 + *x = Token{}
  112 + if protoimpl.UnsafeEnabled {
  113 + mi := &file_game_proto_msgTypes[1]
  114 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  115 + ms.StoreMessageInfo(mi)
  116 + }
  117 +}
  118 +
  119 +func (x *Token) String() string {
  120 + return protoimpl.X.MessageStringOf(x)
  121 +}
  122 +
  123 +func (*Token) ProtoMessage() {}
  124 +
  125 +func (x *Token) ProtoReflect() protoreflect.Message {
  126 + mi := &file_game_proto_msgTypes[1]
  127 + if protoimpl.UnsafeEnabled && x != nil {
  128 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  129 + if ms.LoadMessageInfo() == nil {
  130 + ms.StoreMessageInfo(mi)
  131 + }
  132 + return ms
  133 + }
  134 + return mi.MessageOf(x)
  135 +}
  136 +
  137 +// Deprecated: Use Token.ProtoReflect.Descriptor instead.
  138 +func (*Token) Descriptor() ([]byte, []int) {
  139 + return file_game_proto_rawDescGZIP(), []int{1}
  140 +}
  141 +
  142 +func (x *Token) GetToken() string {
  143 + if x != nil {
  144 + return x.Token
  145 + }
  146 + return ""
  147 +}
  148 +
  149 +type RoleRsp struct {
  150 + state protoimpl.MessageState
  151 + sizeCache protoimpl.SizeCache
  152 + unknownFields protoimpl.UnknownFields
  153 +
  154 + Rsp *PubRsp `protobuf:"bytes,1,opt,name=rsp,proto3" json:"rsp,omitempty"`
  155 + Role *Role `protobuf:"bytes,2,opt,name=role,proto3" json:"role,omitempty"`
  156 +}
  157 +
  158 +func (x *RoleRsp) Reset() {
  159 + *x = RoleRsp{}
  160 + if protoimpl.UnsafeEnabled {
  161 + mi := &file_game_proto_msgTypes[2]
  162 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  163 + ms.StoreMessageInfo(mi)
  164 + }
  165 +}
  166 +
  167 +func (x *RoleRsp) String() string {
  168 + return protoimpl.X.MessageStringOf(x)
  169 +}
  170 +
  171 +func (*RoleRsp) ProtoMessage() {}
  172 +
  173 +func (x *RoleRsp) ProtoReflect() protoreflect.Message {
  174 + mi := &file_game_proto_msgTypes[2]
  175 + if protoimpl.UnsafeEnabled && x != nil {
  176 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  177 + if ms.LoadMessageInfo() == nil {
  178 + ms.StoreMessageInfo(mi)
  179 + }
  180 + return ms
  181 + }
  182 + return mi.MessageOf(x)
  183 +}
  184 +
  185 +// Deprecated: Use RoleRsp.ProtoReflect.Descriptor instead.
  186 +func (*RoleRsp) Descriptor() ([]byte, []int) {
  187 + return file_game_proto_rawDescGZIP(), []int{2}
  188 +}
  189 +
  190 +func (x *RoleRsp) GetRsp() *PubRsp {
  191 + if x != nil {
  192 + return x.Rsp
  193 + }
  194 + return nil
  195 +}
  196 +
  197 +func (x *RoleRsp) GetRole() *Role {
  198 + if x != nil {
  199 + return x.Role
  200 + }
  201 + return nil
  202 +}
  203 +
  204 +var File_game_proto protoreflect.FileDescriptor
  205 +
  206 +var file_game_proto_rawDesc = []byte{
  207 + 0x0a, 0x0a, 0x67, 0x61, 0x6d, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x67, 0x61,
  208 + 0x6d, 0x65, 0x1a, 0x0c, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
  209 + 0x22, 0x75, 0x0a, 0x04, 0x52, 0x6f, 0x6c, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01,
  210 + 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x65, 0x76, 0x65,
  211 + 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x1d,
  212 + 0x0a, 0x0a, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01,
  213 + 0x28, 0x03, 0x52, 0x09, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x16, 0x0a,
  214 + 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64,
  215 + 0x65, 0x76, 0x69, 0x63, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x06, 0x20, 0x01,
  216 + 0x28, 0x03, 0x52, 0x03, 0x75, 0x69, 0x64, 0x22, 0x1d, 0x0a, 0x05, 0x54, 0x6f, 0x6b, 0x65, 0x6e,
  217 + 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
  218 + 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x22, 0x4b, 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73,
  219 + 0x70, 0x12, 0x20, 0x0a, 0x03, 0x72, 0x73, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e,
  220 + 0x2e, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x2e, 0x50, 0x75, 0x62, 0x52, 0x73, 0x70, 0x52, 0x03,
  221 + 0x72, 0x73, 0x70, 0x12, 0x1e, 0x0a, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28,
  222 + 0x0b, 0x32, 0x0a, 0x2e, 0x67, 0x61, 0x6d, 0x65, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x04, 0x72,
  223 + 0x6f, 0x6c, 0x65, 0x32, 0x9a, 0x01, 0x0a, 0x04, 0x47, 0x61, 0x6d, 0x65, 0x12, 0x31, 0x0a, 0x10,
  224 + 0x48, 0x65, 0x61, 0x72, 0x74, 0x42, 0x65, 0x61, 0x74, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72,
  225 + 0x12, 0x0b, 0x2e, 0x67, 0x61, 0x6d, 0x65, 0x2e, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x1a, 0x0e, 0x2e,
  226 + 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x2e, 0x50, 0x75, 0x62, 0x52, 0x73, 0x70, 0x22, 0x00, 0x12,
  227 + 0x2c, 0x0a, 0x0c, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x12,
  228 + 0x0b, 0x2e, 0x67, 0x61, 0x6d, 0x65, 0x2e, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x1a, 0x0d, 0x2e, 0x67,
  229 + 0x61, 0x6d, 0x65, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70, 0x22, 0x00, 0x12, 0x31, 0x0a,
  230 + 0x11, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x6f, 0x6c, 0x65, 0x48, 0x61, 0x6e, 0x64, 0x6c,
  231 + 0x65, 0x72, 0x12, 0x0b, 0x2e, 0x67, 0x61, 0x6d, 0x65, 0x2e, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x1a,
  232 + 0x0d, 0x2e, 0x67, 0x61, 0x6d, 0x65, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70, 0x22, 0x00,
  233 + 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f,
  234 + 0x74, 0x6f, 0x33,
  235 +}
  236 +
  237 +var (
  238 + file_game_proto_rawDescOnce sync.Once
  239 + file_game_proto_rawDescData = file_game_proto_rawDesc
  240 +)
  241 +
  242 +func file_game_proto_rawDescGZIP() []byte {
  243 + file_game_proto_rawDescOnce.Do(func() {
  244 + file_game_proto_rawDescData = protoimpl.X.CompressGZIP(file_game_proto_rawDescData)
  245 + })
  246 + return file_game_proto_rawDescData
  247 +}
  248 +
  249 +var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
  250 +var file_game_proto_goTypes = []interface{}{
  251 + (*Role)(nil), // 0: game.Role
  252 + (*Token)(nil), // 1: game.Token
  253 + (*RoleRsp)(nil), // 2: game.RoleRsp
  254 + (*PubRsp)(nil), // 3: public.PubRsp
  255 +}
  256 +var file_game_proto_depIdxs = []int32{
  257 + 3, // 0: game.RoleRsp.rsp:type_name -> public.PubRsp
  258 + 0, // 1: game.RoleRsp.role:type_name -> game.Role
  259 + 1, // 2: game.Game.HeartBeatHandler:input_type -> game.Token
  260 + 1, // 3: game.Game.LoginHandler:input_type -> game.Token
  261 + 1, // 4: game.Game.CreateRoleHandler:input_type -> game.Token
  262 + 3, // 5: game.Game.HeartBeatHandler:output_type -> public.PubRsp
  263 + 2, // 6: game.Game.LoginHandler:output_type -> game.RoleRsp
  264 + 2, // 7: game.Game.CreateRoleHandler:output_type -> game.RoleRsp
  265 + 5, // [5:8] is the sub-list for method output_type
  266 + 2, // [2:5] is the sub-list for method input_type
  267 + 2, // [2:2] is the sub-list for extension type_name
  268 + 2, // [2:2] is the sub-list for extension extendee
  269 + 0, // [0:2] is the sub-list for field type_name
  270 +}
  271 +
  272 +func init() { file_game_proto_init() }
  273 +func file_game_proto_init() {
  274 + if File_game_proto != nil {
  275 + return
  276 + }
  277 + file_public_proto_init()
  278 + if !protoimpl.UnsafeEnabled {
  279 + file_game_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
  280 + switch v := v.(*Role); i {
  281 + case 0:
  282 + return &v.state
  283 + case 1:
  284 + return &v.sizeCache
  285 + case 2:
  286 + return &v.unknownFields
  287 + default:
  288 + return nil
  289 + }
  290 + }
  291 + file_game_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
  292 + switch v := v.(*Token); i {
  293 + case 0:
  294 + return &v.state
  295 + case 1:
  296 + return &v.sizeCache
  297 + case 2:
  298 + return &v.unknownFields
  299 + default:
  300 + return nil
  301 + }
  302 + }
  303 + file_game_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
  304 + switch v := v.(*RoleRsp); i {
  305 + case 0:
  306 + return &v.state
  307 + case 1:
  308 + return &v.sizeCache
  309 + case 2:
  310 + return &v.unknownFields
  311 + default:
  312 + return nil
  313 + }
  314 + }
  315 + }
  316 + type x struct{}
  317 + out := protoimpl.TypeBuilder{
  318 + File: protoimpl.DescBuilder{
  319 + GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
  320 + RawDescriptor: file_game_proto_rawDesc,
  321 + NumEnums: 0,
  322 + NumMessages: 3,
  323 + NumExtensions: 0,
  324 + NumServices: 1,
  325 + },
  326 + GoTypes: file_game_proto_goTypes,
  327 + DependencyIndexes: file_game_proto_depIdxs,
  328 + MessageInfos: file_game_proto_msgTypes,
  329 + }.Build()
  330 + File_game_proto = out.File
  331 + file_game_proto_rawDesc = nil
  332 + file_game_proto_goTypes = nil
  333 + file_game_proto_depIdxs = nil
  334 +}
... ...
protos/pb/game_grpc.pb.go 0 → 100644
  1 +++ a/protos/pb/game_grpc.pb.go
... ... @@ -0,0 +1,177 @@
  1 +// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
  2 +// versions:
  3 +// - protoc-gen-go-grpc v1.2.0
  4 +// - protoc v3.17.3
  5 +// source: game.proto
  6 +
  7 +package pb
  8 +
  9 +import (
  10 + context "context"
  11 + grpc "google.golang.org/grpc"
  12 + codes "google.golang.org/grpc/codes"
  13 + status "google.golang.org/grpc/status"
  14 +)
  15 +
  16 +// This is a compile-time assertion to ensure that this generated file
  17 +// is compatible with the grpc package it is being compiled against.
  18 +// Requires gRPC-Go v1.32.0 or later.
  19 +const _ = grpc.SupportPackageIsVersion7
  20 +
  21 +// GameClient is the client API for Game service.
  22 +//
  23 +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
  24 +type GameClient interface {
  25 + HeartBeatHandler(ctx context.Context, in *Token, opts ...grpc.CallOption) (*PubRsp, error)
  26 + LoginHandler(ctx context.Context, in *Token, opts ...grpc.CallOption) (*RoleRsp, error)
  27 + CreateRoleHandler(ctx context.Context, in *Token, opts ...grpc.CallOption) (*RoleRsp, error)
  28 +}
  29 +
  30 +type gameClient struct {
  31 + cc grpc.ClientConnInterface
  32 +}
  33 +
  34 +func NewGameClient(cc grpc.ClientConnInterface) GameClient {
  35 + return &gameClient{cc}
  36 +}
  37 +
  38 +func (c *gameClient) HeartBeatHandler(ctx context.Context, in *Token, opts ...grpc.CallOption) (*PubRsp, error) {
  39 + out := new(PubRsp)
  40 + err := c.cc.Invoke(ctx, "/game.Game/HeartBeatHandler", in, out, opts...)
  41 + if err != nil {
  42 + return nil, err
  43 + }
  44 + return out, nil
  45 +}
  46 +
  47 +func (c *gameClient) LoginHandler(ctx context.Context, in *Token, opts ...grpc.CallOption) (*RoleRsp, error) {
  48 + out := new(RoleRsp)
  49 + err := c.cc.Invoke(ctx, "/game.Game/LoginHandler", in, out, opts...)
  50 + if err != nil {
  51 + return nil, err
  52 + }
  53 + return out, nil
  54 +}
  55 +
  56 +func (c *gameClient) CreateRoleHandler(ctx context.Context, in *Token, opts ...grpc.CallOption) (*RoleRsp, error) {
  57 + out := new(RoleRsp)
  58 + err := c.cc.Invoke(ctx, "/game.Game/CreateRoleHandler", in, out, opts...)
  59 + if err != nil {
  60 + return nil, err
  61 + }
  62 + return out, nil
  63 +}
  64 +
  65 +// GameServer is the server API for Game service.
  66 +// All implementations must embed UnimplementedGameServer
  67 +// for forward compatibility
  68 +type GameServer interface {
  69 + HeartBeatHandler(context.Context, *Token) (*PubRsp, error)
  70 + LoginHandler(context.Context, *Token) (*RoleRsp, error)
  71 + CreateRoleHandler(context.Context, *Token) (*RoleRsp, error)
  72 + mustEmbedUnimplementedGameServer()
  73 +}
  74 +
  75 +// UnimplementedGameServer must be embedded to have forward compatible implementations.
  76 +type UnimplementedGameServer struct {
  77 +}
  78 +
  79 +func (UnimplementedGameServer) HeartBeatHandler(context.Context, *Token) (*PubRsp, error) {
  80 + return nil, status.Errorf(codes.Unimplemented, "method HeartBeatHandler not implemented")
  81 +}
  82 +func (UnimplementedGameServer) LoginHandler(context.Context, *Token) (*RoleRsp, error) {
  83 + return nil, status.Errorf(codes.Unimplemented, "method LoginHandler not implemented")
  84 +}
  85 +func (UnimplementedGameServer) CreateRoleHandler(context.Context, *Token) (*RoleRsp, error) {
  86 + return nil, status.Errorf(codes.Unimplemented, "method CreateRoleHandler not implemented")
  87 +}
  88 +func (UnimplementedGameServer) mustEmbedUnimplementedGameServer() {}
  89 +
  90 +// UnsafeGameServer may be embedded to opt out of forward compatibility for this service.
  91 +// Use of this interface is not recommended, as added methods to GameServer will
  92 +// result in compilation errors.
  93 +type UnsafeGameServer interface {
  94 + mustEmbedUnimplementedGameServer()
  95 +}
  96 +
  97 +func RegisterGameServer(s grpc.ServiceRegistrar, srv GameServer) {
  98 + s.RegisterService(&Game_ServiceDesc, srv)
  99 +}
  100 +
  101 +func _Game_HeartBeatHandler_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
  102 + in := new(Token)
  103 + if err := dec(in); err != nil {
  104 + return nil, err
  105 + }
  106 + if interceptor == nil {
  107 + return srv.(GameServer).HeartBeatHandler(ctx, in)
  108 + }
  109 + info := &grpc.UnaryServerInfo{
  110 + Server: srv,
  111 + FullMethod: "/game.Game/HeartBeatHandler",
  112 + }
  113 + handler := func(ctx context.Context, req interface{}) (interface{}, error) {
  114 + return srv.(GameServer).HeartBeatHandler(ctx, req.(*Token))
  115 + }
  116 + return interceptor(ctx, in, info, handler)
  117 +}
  118 +
  119 +func _Game_LoginHandler_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
  120 + in := new(Token)
  121 + if err := dec(in); err != nil {
  122 + return nil, err
  123 + }
  124 + if interceptor == nil {
  125 + return srv.(GameServer).LoginHandler(ctx, in)
  126 + }
  127 + info := &grpc.UnaryServerInfo{
  128 + Server: srv,
  129 + FullMethod: "/game.Game/LoginHandler",
  130 + }
  131 + handler := func(ctx context.Context, req interface{}) (interface{}, error) {
  132 + return srv.(GameServer).LoginHandler(ctx, req.(*Token))
  133 + }
  134 + return interceptor(ctx, in, info, handler)
  135 +}
  136 +
  137 +func _Game_CreateRoleHandler_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
  138 + in := new(Token)
  139 + if err := dec(in); err != nil {
  140 + return nil, err
  141 + }
  142 + if interceptor == nil {
  143 + return srv.(GameServer).CreateRoleHandler(ctx, in)
  144 + }
  145 + info := &grpc.UnaryServerInfo{
  146 + Server: srv,
  147 + FullMethod: "/game.Game/CreateRoleHandler",
  148 + }
  149 + handler := func(ctx context.Context, req interface{}) (interface{}, error) {
  150 + return srv.(GameServer).CreateRoleHandler(ctx, req.(*Token))
  151 + }
  152 + return interceptor(ctx, in, info, handler)
  153 +}
  154 +
  155 +// Game_ServiceDesc is the grpc.ServiceDesc for Game service.
  156 +// It's only intended for direct use with grpc.RegisterService,
  157 +// and not to be introspected or modified (even as a copy)
  158 +var Game_ServiceDesc = grpc.ServiceDesc{
  159 + ServiceName: "game.Game",
  160 + HandlerType: (*GameServer)(nil),
  161 + Methods: []grpc.MethodDesc{
  162 + {
  163 + MethodName: "HeartBeatHandler",
  164 + Handler: _Game_HeartBeatHandler_Handler,
  165 + },
  166 + {
  167 + MethodName: "LoginHandler",
  168 + Handler: _Game_LoginHandler_Handler,
  169 + },
  170 + {
  171 + MethodName: "CreateRoleHandler",
  172 + Handler: _Game_CreateRoleHandler_Handler,
  173 + },
  174 + },
  175 + Streams: []grpc.StreamDesc{},
  176 + Metadata: "game.proto",
  177 +}
... ...
protos/pb/public.pb.go 0 → 100644
  1 +++ a/protos/pb/public.pb.go
... ... @@ -0,0 +1,212 @@
  1 +// Code generated by protoc-gen-go. DO NOT EDIT.
  2 +// versions:
  3 +// protoc-gen-go v1.26.0
  4 +// protoc v3.17.3
  5 +// source: public.proto
  6 +
  7 +package pb
  8 +
  9 +import (
  10 + protoreflect "google.golang.org/protobuf/reflect/protoreflect"
  11 + protoimpl "google.golang.org/protobuf/runtime/protoimpl"
  12 + reflect "reflect"
  13 + sync "sync"
  14 +)
  15 +
  16 +const (
  17 + // Verify that this generated code is sufficiently up-to-date.
  18 + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
  19 + // Verify that runtime/protoimpl is sufficiently up-to-date.
  20 + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
  21 +)
  22 +
  23 +type PubRsp struct {
  24 + state protoimpl.MessageState
  25 + sizeCache protoimpl.SizeCache
  26 + unknownFields protoimpl.UnknownFields
  27 +
  28 + Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
  29 + Msg string `protobuf:"bytes,2,opt,name=msg,proto3" json:"msg,omitempty"`
  30 +}
  31 +
  32 +func (x *PubRsp) Reset() {
  33 + *x = PubRsp{}
  34 + if protoimpl.UnsafeEnabled {
  35 + mi := &file_public_proto_msgTypes[0]
  36 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  37 + ms.StoreMessageInfo(mi)
  38 + }
  39 +}
  40 +
  41 +func (x *PubRsp) String() string {
  42 + return protoimpl.X.MessageStringOf(x)
  43 +}
  44 +
  45 +func (*PubRsp) ProtoMessage() {}
  46 +
  47 +func (x *PubRsp) ProtoReflect() protoreflect.Message {
  48 + mi := &file_public_proto_msgTypes[0]
  49 + if protoimpl.UnsafeEnabled && x != nil {
  50 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  51 + if ms.LoadMessageInfo() == nil {
  52 + ms.StoreMessageInfo(mi)
  53 + }
  54 + return ms
  55 + }
  56 + return mi.MessageOf(x)
  57 +}
  58 +
  59 +// Deprecated: Use PubRsp.ProtoReflect.Descriptor instead.
  60 +func (*PubRsp) Descriptor() ([]byte, []int) {
  61 + return file_public_proto_rawDescGZIP(), []int{0}
  62 +}
  63 +
  64 +func (x *PubRsp) GetCode() int32 {
  65 + if x != nil {
  66 + return x.Code
  67 + }
  68 + return 0
  69 +}
  70 +
  71 +func (x *PubRsp) GetMsg() string {
  72 + if x != nil {
  73 + return x.Msg
  74 + }
  75 + return ""
  76 +}
  77 +
  78 +type GenerateToken struct {
  79 + state protoimpl.MessageState
  80 + sizeCache protoimpl.SizeCache
  81 + unknownFields protoimpl.UnknownFields
  82 +
  83 + Phone string `protobuf:"bytes,1,opt,name=phone,proto3" json:"phone,omitempty"`
  84 +}
  85 +
  86 +func (x *GenerateToken) Reset() {
  87 + *x = GenerateToken{}
  88 + if protoimpl.UnsafeEnabled {
  89 + mi := &file_public_proto_msgTypes[1]
  90 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  91 + ms.StoreMessageInfo(mi)
  92 + }
  93 +}
  94 +
  95 +func (x *GenerateToken) String() string {
  96 + return protoimpl.X.MessageStringOf(x)
  97 +}
  98 +
  99 +func (*GenerateToken) ProtoMessage() {}
  100 +
  101 +func (x *GenerateToken) ProtoReflect() protoreflect.Message {
  102 + mi := &file_public_proto_msgTypes[1]
  103 + if protoimpl.UnsafeEnabled && x != nil {
  104 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  105 + if ms.LoadMessageInfo() == nil {
  106 + ms.StoreMessageInfo(mi)
  107 + }
  108 + return ms
  109 + }
  110 + return mi.MessageOf(x)
  111 +}
  112 +
  113 +// Deprecated: Use GenerateToken.ProtoReflect.Descriptor instead.
  114 +func (*GenerateToken) Descriptor() ([]byte, []int) {
  115 + return file_public_proto_rawDescGZIP(), []int{1}
  116 +}
  117 +
  118 +func (x *GenerateToken) GetPhone() string {
  119 + if x != nil {
  120 + return x.Phone
  121 + }
  122 + return ""
  123 +}
  124 +
  125 +var File_public_proto protoreflect.FileDescriptor
  126 +
  127 +var file_public_proto_rawDesc = []byte{
  128 + 0x0a, 0x0c, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06,
  129 + 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x22, 0x2e, 0x0a, 0x06, 0x50, 0x75, 0x62, 0x52, 0x73, 0x70,
  130 + 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04,
  131 + 0x63, 0x6f, 0x64, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28,
  132 + 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x22, 0x25, 0x0a, 0x0d, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61,
  133 + 0x74, 0x65, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x68, 0x6f, 0x6e, 0x65,
  134 + 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x42, 0x09, 0x5a,
  135 + 0x07, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
  136 +}
  137 +
  138 +var (
  139 + file_public_proto_rawDescOnce sync.Once
  140 + file_public_proto_rawDescData = file_public_proto_rawDesc
  141 +)
  142 +
  143 +func file_public_proto_rawDescGZIP() []byte {
  144 + file_public_proto_rawDescOnce.Do(func() {
  145 + file_public_proto_rawDescData = protoimpl.X.CompressGZIP(file_public_proto_rawDescData)
  146 + })
  147 + return file_public_proto_rawDescData
  148 +}
  149 +
  150 +var file_public_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
  151 +var file_public_proto_goTypes = []interface{}{
  152 + (*PubRsp)(nil), // 0: public.PubRsp
  153 + (*GenerateToken)(nil), // 1: public.GenerateToken
  154 +}
  155 +var file_public_proto_depIdxs = []int32{
  156 + 0, // [0:0] is the sub-list for method output_type
  157 + 0, // [0:0] is the sub-list for method input_type
  158 + 0, // [0:0] is the sub-list for extension type_name
  159 + 0, // [0:0] is the sub-list for extension extendee
  160 + 0, // [0:0] is the sub-list for field type_name
  161 +}
  162 +
  163 +func init() { file_public_proto_init() }
  164 +func file_public_proto_init() {
  165 + if File_public_proto != nil {
  166 + return
  167 + }
  168 + if !protoimpl.UnsafeEnabled {
  169 + file_public_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
  170 + switch v := v.(*PubRsp); i {
  171 + case 0:
  172 + return &v.state
  173 + case 1:
  174 + return &v.sizeCache
  175 + case 2:
  176 + return &v.unknownFields
  177 + default:
  178 + return nil
  179 + }
  180 + }
  181 + file_public_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
  182 + switch v := v.(*GenerateToken); i {
  183 + case 0:
  184 + return &v.state
  185 + case 1:
  186 + return &v.sizeCache
  187 + case 2:
  188 + return &v.unknownFields
  189 + default:
  190 + return nil
  191 + }
  192 + }
  193 + }
  194 + type x struct{}
  195 + out := protoimpl.TypeBuilder{
  196 + File: protoimpl.DescBuilder{
  197 + GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
  198 + RawDescriptor: file_public_proto_rawDesc,
  199 + NumEnums: 0,
  200 + NumMessages: 2,
  201 + NumExtensions: 0,
  202 + NumServices: 0,
  203 + },
  204 + GoTypes: file_public_proto_goTypes,
  205 + DependencyIndexes: file_public_proto_depIdxs,
  206 + MessageInfos: file_public_proto_msgTypes,
  207 + }.Build()
  208 + File_public_proto = out.File
  209 + file_public_proto_rawDesc = nil
  210 + file_public_proto_goTypes = nil
  211 + file_public_proto_depIdxs = nil
  212 +}
... ...
protos/public.proto 0 → 100644
  1 +++ a/protos/public.proto
... ... @@ -0,0 +1,13 @@
  1 +syntax = "proto3";
  2 +package public;
  3 +
  4 +option go_package = "./pb;pb";
  5 +
  6 +message PubRsp {
  7 + int32 code = 1;
  8 + string msg = 2;
  9 +}
  10 +
  11 +message GenerateToken {
  12 + string phone = 1;
  13 +}
0 14 \ No newline at end of file
... ...
test/client.go 0 → 100644
  1 +++ a/test/client.go
... ... @@ -0,0 +1,80 @@
  1 +package main
  2 +
  3 +import (
  4 + "context"
  5 + "fmt"
  6 + "google.golang.org/grpc"
  7 + _ "pro2d/conf"
  8 + "pro2d/protos/pb"
  9 + "pro2d/utils"
  10 +)
  11 +
  12 +func Register(c pb.AccountClient, phone, password string) error {
  13 + r, err := c.RegisterHandler(context.Background(), &pb.Register{
  14 + Phone: "17683852936",
  15 + Password: "123456",
  16 + Code: 0,
  17 + })
  18 +
  19 + if err != nil {
  20 + return err
  21 + }
  22 + if r.Code != 0 {
  23 + return fmt.Errorf("%s", r.Msg)
  24 + }
  25 + utils.Sugar.Debug("register success")
  26 + return nil
  27 +}
  28 +
  29 +func Login(loginUri, token string) {
  30 + gameConn, err := grpc.Dial(loginUri, grpc.WithInsecure() )
  31 + if err != nil {
  32 + utils.Sugar.Errorf("game conn err: %v", err)
  33 + return
  34 + }
  35 + defer gameConn.Close()
  36 +
  37 + client:= pb.NewGameClient(gameConn)
  38 + loginRsp, err := client.LoginHandler(context.Background(), &pb.Token{
  39 + Token: token,
  40 + })
  41 +
  42 + if err != nil {
  43 + utils.Sugar.Errorf("login error: %v", err)
  44 + return
  45 + }
  46 +
  47 + print(loginRsp)
  48 +
  49 + createRole, err := client.CreateRoleHandler(context.Background(), &pb.Token{Token: token})
  50 + if err != nil {
  51 + utils.Sugar.Errorf("create role err: %v", err)
  52 + return
  53 + }
  54 + print(createRole)
  55 +
  56 + utils.Sugar.Debug("login rsp: ", createRole.Rsp.Code, createRole.Rsp.Msg)
  57 +}
  58 +
  59 +func main() {
  60 + conn, err := grpc.Dial("localhost:8848", grpc.WithInsecure())
  61 + if err != nil {
  62 + utils.Sugar.Errorf("conn err: %v", err)
  63 + return
  64 + }
  65 + defer conn.Close()
  66 + c := pb.NewAccountClient(conn)
  67 + //err = Register(c,"17683852936", "123456")
  68 + //if err != nil {
  69 + // utils.Sugar.Errorf("register err: %v", err)
  70 + // return
  71 + //}
  72 + rsp, err := c.CreateTokenHandler(context.Background(), &pb.AccountInfo{
  73 + Phone: "17683852936",
  74 + Password: "123456",
  75 + })
  76 +
  77 + Login(rsp.GameService.Address, rsp.Token)
  78 +
  79 +
  80 +}
... ...
utils/common.go 0 → 100644
  1 +++ a/utils/common.go
... ... @@ -0,0 +1,11 @@
  1 +package utils
  2 +
  3 +const (
  4 + Pro2DTokenSignedString = "Pro2DSecret"
  5 +
  6 + ACCOUNTDB = "account"
  7 + ACCOUNT = "account"
  8 +
  9 + GAMEDB = "game"
  10 + ROLET = "role"
  11 +)
... ...
utils/jwt.go 0 → 100644
  1 +++ a/utils/jwt.go
... ... @@ -0,0 +1,98 @@
  1 +package utils
  2 +
  3 +import (
  4 + "context"
  5 + "fmt"
  6 + "pro2d/protos/pb"
  7 + "time"
  8 +
  9 + jwt "github.com/dgrijalva/jwt-go"
  10 + "google.golang.org/grpc/metadata"
  11 +)
  12 +
  13 +func CreateToken(account *pb.AccountInfo) (tokenString string) {
  14 + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
  15 + "iss": "pro2d-app-server",
  16 + "aud": "pro2d-app-server",
  17 + "nbf": time.Now().Unix(),
  18 + "exp": time.Now().Add(time.Hour).Unix(),
  19 + "sub": "pro2d",
  20 + "phone": account.Phone,
  21 + "uid": account.Uid,
  22 + "device": account.Device,
  23 + })
  24 + tokenString, err := token.SignedString([]byte(Pro2DTokenSignedString))
  25 + if err != nil {
  26 + panic(err)
  27 + }
  28 + return tokenString
  29 +}
  30 +
  31 +func ParseToken(tokenStr string)*pb.AccountInfo {
  32 + var clientClaims Claims
  33 + token, err := jwt.ParseWithClaims(tokenStr, &clientClaims, func(token *jwt.Token) (interface{}, error) {
  34 + if token.Header["alg"] != "HS256" {
  35 + //panic("ErrInvalidAlgorithm")
  36 + Sugar.Error("ErrInvalidAlgorithm")
  37 + return nil, nil
  38 + }
  39 + return []byte(Pro2DTokenSignedString), nil
  40 + })
  41 + if err != nil {
  42 + Sugar.Error("jwt parse error")
  43 + return nil
  44 + }
  45 +
  46 + if !token.Valid {
  47 + Sugar.Error("ErrInvalidToken")
  48 + return nil
  49 + }
  50 + return &clientClaims.AccountInfo
  51 +}
  52 +
  53 +// AuthToken 自定义认证
  54 +type AuthToken struct {
  55 + Token string
  56 +}
  57 +
  58 +func (c AuthToken) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
  59 + return map[string]string{
  60 + "authorization": c.Token,
  61 + }, nil
  62 +}
  63 +
  64 +func (c AuthToken) RequireTransportSecurity() bool {
  65 + return false
  66 +}
  67 +
  68 +// Claims defines the struct containing the token claims.
  69 +type Claims struct {
  70 + jwt.StandardClaims
  71 + //phone string `json:"phone"`
  72 + //uid int64 `json:"uid"`
  73 + //device string `json:"device"`
  74 + pb.AccountInfo
  75 +}
  76 +
  77 +// 从 context 的 metadata 中,取出 token
  78 +func getTokenFromContext(ctx context.Context) (string, error) {
  79 + md, ok := metadata.FromIncomingContext(ctx)
  80 + if !ok {
  81 + return "", fmt.Errorf("ErrNoMetadataInContext")
  82 + }
  83 + // md 的类型是 type MD map[string][]string
  84 + token, ok := md["authorization"]
  85 + if !ok || len(token) == 0 {
  86 + return "", fmt.Errorf("ErrNoAuthorizationInMetadata")
  87 + }
  88 + // 因此,token 是一个字符串数组,我们只用了 token[0]
  89 + return token[0], nil
  90 +}
  91 +
  92 +func CheckAuth(ctx context.Context) *pb.AccountInfo {
  93 + tokenStr, err := getTokenFromContext(ctx)
  94 + if err != nil {
  95 + panic("get token from context error")
  96 + }
  97 + return ParseToken(tokenStr)
  98 +}
0 99 \ No newline at end of file
... ...
utils/jwt_test.go 0 → 100644
  1 +++ a/utils/jwt_test.go
... ... @@ -0,0 +1,19 @@
  1 +package utils
  2 +
  3 +import (
  4 + "fmt"
  5 + "pro2d/protos/pb"
  6 + "testing"
  7 +)
  8 +
  9 +func TestCreateToken(t *testing.T) {
  10 + account := &pb.AccountInfo{
  11 + Phone: "17683852936",
  12 + Password: "123456",
  13 + Uid: 12312,
  14 + Device: "12312312",
  15 + }
  16 + token := CreateToken(account)
  17 + ac := ParseToken(token)
  18 + fmt.Println("token: ", token, "\nac: ", ac)
  19 +}
... ...
utils/logger.go 0 → 100644
  1 +++ a/utils/logger.go
... ... @@ -0,0 +1,107 @@
  1 +package utils
  2 +
  3 +import (
  4 + "go.uber.org/zap"
  5 + "go.uber.org/zap/zapcore"
  6 + lumberjack "gopkg.in/natefinch/lumberjack.v2"
  7 + "net/http"
  8 + "os"
  9 +)
  10 +
  11 +var Sugar *zap.SugaredLogger
  12 +var Logger *zap.Logger
  13 +
  14 +func InitLogger(conf *lumberjack.Logger) {
  15 + writeSyncer := zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(conf)) //控制台和日志同时输出
  16 + encoder := getEncoder()
  17 + core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel)
  18 +
  19 + Logger = zap.New(core, zap.AddCaller())
  20 + Sugar = Logger.Sugar()
  21 +}
  22 +
  23 +func getEncoder() zapcore.Encoder {
  24 + encoderConfig := zap.NewProductionEncoderConfig()
  25 + encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
  26 + encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
  27 + return zapcore.NewConsoleEncoder(encoderConfig)
  28 +}
  29 +
  30 +func simpleHttpGet(url string) {
  31 + Sugar.Debugf("Trying to hit GET request for %s", url)
  32 + resp, err := http.Get(url)
  33 + if err != nil {
  34 + Sugar.Errorf("Error fetching URL %s : Error = %s", url, err)
  35 + } else {
  36 + Sugar.Infof("Success! statusCode = %s for URL %s", resp.Status, url)
  37 + resp.Body.Close()
  38 + }
  39 +}
  40 +
  41 +func LogTest() {
  42 + lumberJackLogger := &lumberjack.Logger{
  43 + Filename: "./pro2d.log", // ⽇志⽂件路径
  44 + MaxSize: 1024, // 1M=1024KB=1024000byte
  45 + MaxBackups: 5, // 最多保留5个备份
  46 + MaxAge: 30, // days
  47 + Compress: true, // 是否压缩 disabled by default
  48 + }
  49 +
  50 + InitLogger(lumberJackLogger)
  51 + defer Logger.Sync()
  52 + for i:=0; i < 10000;i++ {
  53 + simpleHttpGet("www.baidu.com")
  54 + simpleHttpGet("http://www.baidu.com")
  55 + }
  56 +}
  57 +
  58 +//--使用sink 结合es使用
  59 +//func registerSinkDemo() {
  60 +// zap.RegisterSink("mq", mq.NewMqSink)
  61 +// writer, close, err := zap.Open("mq://192.168.99.100:9876/log")
  62 +// if err != nil {
  63 +// panic(err)
  64 +// }
  65 +// defer close()
  66 +// logger := zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), writer, zap.DebugLevel)).Sugar()
  67 +// logger.Info("hello")
  68 +//}
  69 +//
  70 +//type MqWriteSyncer struct {
  71 +// topic string
  72 +// producer rocketmq.Producer
  73 +// ctx context.Context
  74 +//}
  75 +//
  76 +//func (m *MqWriteSyncer) Close() error {
  77 +// return m.producer.Shutdown()
  78 +//}
  79 +//
  80 +//func (m *MqWriteSyncer) Write(p []byte) (n int, err error) {
  81 +// msg := &primitive.Message{
  82 +// Topic: m.topic,
  83 +// Body: p,
  84 +// }
  85 +// err = m.producer.SendOneWay(m.ctx, msg)
  86 +// return len(p), err
  87 +//}
  88 +//
  89 +//func (m *MqWriteSyncer) Sync() error {
  90 +// return nil
  91 +//}
  92 +//
  93 +//func NewMqSink(url *url.URL) (zap.Sink, error) {
  94 +// broker := fmt.Sprintf("%s:%s", url.Hostname(), url.Port())
  95 +// topic := url.Path[1:len(url.Path)]
  96 +// p, _ := rocketmq.NewProducer(
  97 +// producer.WithNameServer([]string{broker}),
  98 +// producer.WithRetry(2),
  99 +// )
  100 +// err := p.Start()
  101 +// if err != nil {
  102 +// fmt.Printf("start producer error: %s", err.Error())
  103 +// return nil, err
  104 +// }
  105 +//
  106 +// return &MqWriteSyncer{producer: p, ctx: context.Background(), topic: topic}, nil
  107 +//}
0 108 \ No newline at end of file
... ...
utils/md5.go 0 → 100644
  1 +++ a/utils/md5.go
... ... @@ -0,0 +1,12 @@
  1 +package utils
  2 +
  3 +import (
  4 + "crypto/md5"
  5 + "encoding/hex"
  6 +)
  7 +
  8 +func Md5V(str string) string {
  9 + h := md5.New()
  10 + h.Write([]byte(str))
  11 + return hex.EncodeToString(h.Sum(nil))
  12 +}
0 13 \ No newline at end of file
... ...
utils/snowflake.go 0 → 100644
  1 +++ a/utils/snowflake.go
... ... @@ -0,0 +1,76 @@
  1 +package utils
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/golang/glog"
  6 + "sync"
  7 + "time"
  8 +)
  9 +
  10 +type Snowflake struct {
  11 + *sync.Mutex // 锁
  12 + timestamp int64 // 时间戳 ,毫秒
  13 + workerid int64 // 工作节点
  14 + datacenterid int64 // 数据中心机房id
  15 + sequence int64 // 序列号
  16 +}
  17 +const (
  18 + epoch = int64(1577808000000) // 设置起始时间(时间戳/毫秒):2020-01-01 00:00:00,有效期69年
  19 + timestampBits = uint(41) // 时间戳占用位数
  20 + datacenteridBits = uint(2) // 数据中心id所占位数
  21 + workeridBits = uint(7) // 机器id所占位数
  22 + sequenceBits = uint(12) // 序列所占的位数
  23 + timestampMax = int64(-1 ^ (-1 << timestampBits)) // 时间戳最大值
  24 + datacenteridMax = int64(-1 ^ (-1 << datacenteridBits)) // 支持的最大数据中心id数量
  25 + workeridMax = int64(-1 ^ (-1 << workeridBits)) // 支持的最大机器id数量
  26 + sequenceMask = int64(-1 ^ (-1 << sequenceBits)) // 支持的最大序列id数量
  27 + workeridShift = sequenceBits // 机器id左移位数
  28 + datacenteridShift = sequenceBits + workeridBits // 数据中心id左移位数
  29 + timestampShift = sequenceBits + workeridBits + datacenteridBits // 时间戳左移位数
  30 +)
  31 +
  32 +
  33 +
  34 +func NewSnowflake(workerid, datacenterid int64) *Snowflake {
  35 + return &Snowflake{
  36 + Mutex: new(sync.Mutex),
  37 + timestamp: time.Now().UnixNano() / 1000000,
  38 + workerid: workerid,
  39 + datacenterid: datacenterid,
  40 + sequence: 0,
  41 + }
  42 +}
  43 +
  44 +func (s *Snowflake) NextValStr() string {
  45 + return fmt.Sprintf("%d", s.NextVal())
  46 +}
  47 +
  48 +
  49 +func (s *Snowflake) NextVal() int64 {
  50 + s.Lock()
  51 + now := time.Now().UnixNano() / 1000000 // 转毫秒
  52 + if s.timestamp == now {
  53 + // 当同一时间戳(精度:毫秒)下多次生成id会增加序列号
  54 + s.sequence = (s.sequence + 1) & sequenceMask
  55 + if s.sequence == 0 {
  56 + // 如果当前序列超出12bit长度,则需要等待下一毫秒
  57 + // 下一毫秒将使用sequence:0
  58 + for now <= s.timestamp {
  59 + now = time.Now().UnixNano() / 1000000
  60 + }
  61 + }
  62 + } else {
  63 + // 不同时间戳(精度:毫秒)下直接使用序列号:0
  64 + s.sequence = 0
  65 + }
  66 + t := now - epoch
  67 + if t > timestampMax {
  68 + s.Unlock()
  69 + glog.Errorf("epoch must be between 0 and %d", timestampMax-1)
  70 + return 0
  71 + }
  72 + s.timestamp = now
  73 + r := int64((t)<<timestampShift | (s.datacenterid << datacenteridShift) | (s.workerid << workeridShift) | (s.sequence))
  74 + s.Unlock()
  75 + return r
  76 +}
0 77 \ No newline at end of file
... ...
utils/utils.go 0 → 100644
  1 +++ a/utils/utils.go
... ... @@ -0,0 +1 @@
  1 +package utils
0 2 \ No newline at end of file
... ...