-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
113 lines (93 loc) · 3.38 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package data
import (
"context"
"github.com/beiduoke/go-scaffold/api/common/conf"
coreV1 "github.com/beiduoke/go-scaffold/api/core/service/v1"
"github.com/beiduoke/go-scaffold/pkg/auth/authn"
"github.com/beiduoke/go-scaffold/pkg/auth/authz"
"github.com/beiduoke/go-scaffold/pkg/auth/authz/noop"
"github.com/beiduoke/go-scaffold/pkg/bootstrap"
"github.com/beiduoke/go-scaffold/pkg/service"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"
"github.com/google/wire"
"github.com/redis/go-redis/v9"
)
// ProviderSet is data providers.
var ProviderSet = wire.NewSet(
// 服务注册发现
NewDiscovery,
NewData,
NewRedisClient,
// 认证
NewAuthenticator,
NewAuthTokenRepo,
NewSecurityUser,
// 鉴权
NewAuthorized,
NewUserServiceClient,
NewRoleServiceClient,
NewPostServiceClient,
NewDeptServiceClient,
NewMenuServiceClient,
)
// Data .
type Data struct {
log *log.Helper
rdb *redis.Client
}
// NewData .
func NewData(rdb *redis.Client, logger log.Logger) (*Data, func(), error) {
l := log.NewHelper(log.With(logger, "module", "data/admin-service"))
d := &Data{
rdb: rdb,
log: l,
}
return d, func() {
l.Info("message", "closing the data resources")
if err := d.rdb.Close(); err != nil {
l.Error(err)
}
}, nil
}
// NewRedisClient 创建Redis客户端
func NewRedisClient(cfg *conf.Bootstrap, _ log.Logger) *redis.Client {
// l := log.NewHelper(log.With(logger, "module", "redis/data/admin-service"))
return bootstrap.NewRedisClient(cfg.Data)
}
// NewDiscovery 创建服务发现客户端
func NewDiscovery(cfg *conf.Bootstrap) registry.Discovery {
return bootstrap.NewConsulRegistry(cfg.Registry)
}
// NewAuthenticator 创建认证
func NewAuthenticator(cfg *conf.Bootstrap, logger log.Logger) authn.Authenticator {
return bootstrap.NewJwtAuthenticator(cfg, logger)
}
// NewAuthorized 创建鉴权
func NewAuthorized(logger log.Logger) authz.Authorized {
return noop.State{}
}
// NewUserServiceClient 用户服务
func NewUserServiceClient(r registry.Discovery, c *conf.Bootstrap) coreV1.UserServiceClient {
return coreV1.NewUserServiceClient(bootstrap.CreateGrpcClient(context.Background(), r, service.CoreService, c))
}
// NewAuthorizerServiceClient 认证服务
func NewAuthorizerServiceClient(r registry.Discovery, c *conf.Bootstrap) coreV1.AuthServiceClient {
return coreV1.NewAuthServiceClient(bootstrap.CreateGrpcClient(context.Background(), r, service.CoreService, c))
}
// NewRoleServiceClient 角色服务
func NewRoleServiceClient(r registry.Discovery, c *conf.Bootstrap) coreV1.RoleServiceClient {
return coreV1.NewRoleServiceClient(bootstrap.CreateGrpcClient(context.Background(), r, service.CoreService, c))
}
// NewPostServiceClient 岗位服务
func NewPostServiceClient(r registry.Discovery, c *conf.Bootstrap) coreV1.PostServiceClient {
return coreV1.NewPostServiceClient(bootstrap.CreateGrpcClient(context.Background(), r, service.CoreService, c))
}
// NewDeptServiceClient 部门服务
func NewDeptServiceClient(r registry.Discovery, c *conf.Bootstrap) coreV1.DeptServiceClient {
return coreV1.NewDeptServiceClient(bootstrap.CreateGrpcClient(context.Background(), r, service.CoreService, c))
}
// NewMenuServiceClient 菜单服务
func NewMenuServiceClient(r registry.Discovery, c *conf.Bootstrap) coreV1.MenuServiceClient {
return coreV1.NewMenuServiceClient(bootstrap.CreateGrpcClient(context.Background(), r, service.CoreService, c))
}