github.com/Go-To-Byte/DouSheng/user_center@v0.0.0-20230524130918-ad531c1a3f6a/apps/user/impl/impl.go (about)

     1  // @Author: Ciusyan 2023/1/23
     2  package impl
     3  
     4  import (
     5  	"github.com/infraboard/mcube/logger"
     6  	"github.com/infraboard/mcube/logger/zap"
     7  	"google.golang.org/grpc"
     8  	"gorm.io/gorm"
     9  	"sync"
    10  
    11  	"github.com/Go-To-Byte/DouSheng/api_rooter/apps/token"
    12  	"github.com/Go-To-Byte/DouSheng/api_rooter/client/rpc"
    13  	"github.com/Go-To-Byte/DouSheng/dou_kit/conf"
    14  	"github.com/Go-To-Byte/DouSheng/dou_kit/ioc"
    15  	"github.com/Go-To-Byte/DouSheng/interaction_service/apps/favorite"
    16  	interactionrpc "github.com/Go-To-Byte/DouSheng/interaction_service/client/rpc"
    17  	"github.com/Go-To-Byte/DouSheng/relation_service/apps/relation"
    18  	relationrpc "github.com/Go-To-Byte/DouSheng/relation_service/client/rpc"
    19  	"github.com/Go-To-Byte/DouSheng/video_service/apps/video"
    20  	videoRpc "github.com/Go-To-Byte/DouSheng/video_service/client/rpc"
    21  
    22  	"github.com/Go-To-Byte/DouSheng/user_center/apps/user"
    23  )
    24  
    25  // 用于注入IOC中
    26  var impl = &userServiceImpl{}
    27  
    28  // userServiceImpl 基于Mysql实现的Service
    29  type userServiceImpl struct {
    30  	l  logger.Logger
    31  	db *gorm.DB
    32  
    33  	user.UnimplementedServiceServer
    34  	// 用于管理Token
    35  	video        video.ServiceClient
    36  	relation     relation.ServiceClient
    37  	favorite     favorite.ServiceClient
    38  	tokenService token.ServiceClient
    39  }
    40  
    41  func (u *userServiceImpl) Init() error {
    42  	u.l = zap.L().Named("User")
    43  	errors := make([]error, 0)
    44  
    45  	wait := sync.WaitGroup{}
    46  	wait.Add(1)
    47  	defer wait.Wait()
    48  	go func() {
    49  		defer wait.Done()
    50  		if db, err := conf.C().MySQL.GetDB(); err != nil {
    51  			errors = append(errors, err)
    52  		} else {
    53  			u.db = db
    54  		}
    55  	}()
    56  
    57  	go func() {
    58  		wait.Add(1)
    59  		defer wait.Done()
    60  		if client, err := rpc.NewApiRooterClientFromCfg(); err != nil {
    61  			errors = append(errors, err)
    62  		} else {
    63  			u.tokenService = client.TokenService()
    64  		}
    65  	}()
    66  
    67  	go func() {
    68  		wait.Add(1)
    69  		defer wait.Done()
    70  		if client, err := videoRpc.NewVideoServiceClientFromCfg(); err != nil {
    71  			errors = append(errors, err)
    72  		} else {
    73  			u.video = client.VideoService()
    74  		}
    75  	}()
    76  
    77  	go func() {
    78  		wait.Add(1)
    79  		defer wait.Done()
    80  		if client, err := relationrpc.NewRelationServiceClientFromCfg(); err != nil {
    81  			errors = append(errors, err)
    82  		} else {
    83  			u.relation = client.RelationService()
    84  		}
    85  	}()
    86  
    87  	go func() {
    88  		wait.Add(1)
    89  		defer wait.Done()
    90  		if client, err := interactionrpc.NewInteractionServiceClientFromConfig(); err != nil {
    91  			errors = append(errors, err)
    92  		} else {
    93  			u.favorite = client.FavoriteService()
    94  		}
    95  	}()
    96  
    97  	return nil
    98  }
    99  
   100  func (u *userServiceImpl) Name() string {
   101  	return user.AppName
   102  }
   103  
   104  func (u *userServiceImpl) Registry(s *grpc.Server) {
   105  	user.RegisterServiceServer(s, impl)
   106  }
   107  
   108  func init() {
   109  	// 将此UserService注入GRPC服务的IOC容器中
   110  	ioc.GrpcDI(impl)
   111  }