server.go 3.42 KB
package actions

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
	"google.golang.org/grpc/reflection"
	"pro2d/components/jwt"
	"pro2d/conf"
	"pro2d/models"
	"pro2d/protos/pb"
	"pro2d/utils"
)

type LoginServer struct{
	pb.UnsafeLoginServer
	*BasicServer
}

func NewAccountServer() *LoginServer {
	return &LoginServer{
		BasicServer: NewServer(),
	}
}

//拦截器
func AccountServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler) (interface{}, error) {

	utils.Sugar.Debugf("gRPC method: %s, %v", info.FullMethod, req)
	resp, err := handler(ctx, req)
	return resp, err
}

func (s *LoginServer)Start() error {
	lis, err := s.BasicServer.Start(conf.GlobalConf.AccountConf)
	if err != nil {
		return err
	}

	models.InitAccountServerModels()

	var opts []grpc.ServerOption

	if conf.GlobalConf.TLS.Status {
		//TLS
		creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key")
		if err != nil {
			utils.Sugar.Errorf("Failed to generate credentials %v", err)
			return err
		}
		opts = append(opts, grpc.Creds(creds))
	}

	//拦截器
	opts = append(opts, grpc.UnaryInterceptor(AccountServerInterceptor))

	//new一个grpc
	s.GrpcServer = grpc.NewServer(opts...)

	pb.RegisterLoginServer(s.GrpcServer, s)
	reflection.Register(s.GrpcServer) //在给定的gRPC服务器上注册服务器反射服务

	// Serve方法在lis上接受传入连接,为每个连接创建一个ServerTransport和server的goroutine。
	// 该goroutine读取gRPC请求,然后调用已注册的处理程序来响应它们。
	utils.Sugar.Debugf("Start LoginServer listening on %d with TLS", conf.GlobalConf.AccountConf.Port)

	return s.GrpcServer.Serve(lis)
}

func (s *LoginServer)Stop()  {
	s.BasicServer.Stop()
}

type GameServer struct{
	pb.UnimplementedGameServer
	*BasicServer
}

func NewGameServer() *GameServer {
	return &GameServer{
		BasicServer: NewServer(),
	}
}
//拦截器
func GameServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler) (interface{}, error) {

	//utils.Sugar.Debugf("gRPC method: %s, %v", info.FullMethod, req)
	uid := jwt.CheckAuth(ctx)
	if uid == ""{
		return nil, fmt.Errorf("token error")
	}
	context.WithValue(ctx, "uid", uid)
	resp, err := handler(ctx, req)
	return resp, err
}

func (s *GameServer)Start() error {
	lis, err := s.BasicServer.Start(conf.GlobalConf.GameConf)
	if err != nil {
		return err
	}

	models.InitGameServerModels()

	var opts []grpc.ServerOption
	//TLS
	if conf.GlobalConf.TLS.Status {
		//TLS
		creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key")
		if err != nil {
			utils.Sugar.Errorf("Failed to generate credentials %v", err)
			return err
		}
		opts = append(opts, grpc.Creds(creds))
	}

	//拦截器
	opts = append(opts, grpc.UnaryInterceptor(GameServerInterceptor))

	//new一个grpc
	s.GrpcServer = grpc.NewServer(opts...)

	pb.RegisterGameServer(s.GrpcServer, s)
	reflection.Register(s.GrpcServer) //在给定的gRPC服务器上注册服务器反射服务

	// Serve方法在lis上接受传入连接,为每个连接创建一个ServerTransport和server的goroutine。
	// 该goroutine读取gRPC请求,然后调用已注册的处理程序来响应它们。
	utils.Sugar.Debugf("Start GameServer listening on %d with TLS", conf.GlobalConf.GameConf.Port)
	return s.GrpcServer.Serve(lis)
}

func (s *GameServer)Stop()  {
	s.BasicServer.Stop()
}