43 lines
964 B
Go
43 lines
964 B
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gateway/internal/config"
|
|
"shared/pkg/logger"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
// 导入生成的 proto 代码
|
|
userpb "shared/proto/user"
|
|
)
|
|
|
|
type UserService struct {
|
|
client userpb.UserServiceClient
|
|
}
|
|
|
|
func NewUserService(cfg *config.Config) (*UserService, error) {
|
|
// 连接到用户服务
|
|
conn, err := grpc.Dial(cfg.Services.UserService.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
if err != nil {
|
|
logger.Error("Failed to connect to user service: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// 创建 gRPC 客户端
|
|
client := userpb.NewUserServiceClient(conn)
|
|
|
|
return &UserService{client: client}, nil
|
|
}
|
|
|
|
// Register 用户注册
|
|
func (s *UserService) Register(ctx context.Context, account, password string) (*userpb.RegisterResponse, error) {
|
|
req := &userpb.RegisterRequest{
|
|
Account: account,
|
|
Password: password,
|
|
}
|
|
|
|
return s.client.Register(ctx, req)
|
|
}
|