/
impl.go
81 lines (65 loc) · 1.68 KB
/
impl.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
package grpc
import (
"context"
"fmt"
"github.com/infraboard/mcube/logger"
"github.com/infraboard/mcube/logger/zap"
"github.com/infraboard/mcube/pb/http"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
"github.com/infraboard/keyauth/conf"
"github.com/infraboard/keyauth/pkg"
"github.com/infraboard/keyauth/pkg/namespace"
"github.com/infraboard/keyauth/pkg/policy"
"github.com/infraboard/keyauth/pkg/role"
"github.com/infraboard/keyauth/pkg/user"
)
var (
// Service 服务实例
Service = &service{}
)
type service struct {
log logger.Logger
col *mongo.Collection
enableCache bool
notifyCachPre string
namespace namespace.NamespaceServiceServer
user user.UserServiceServer
role role.RoleServiceServer
policy.UnimplementedPolicyServiceServer
}
func (s *service) Config() error {
if pkg.Namespace == nil {
return fmt.Errorf("dependence namespace service is nil, please load first")
}
s.namespace = pkg.Namespace
if pkg.User == nil {
return fmt.Errorf("dependence user service is nil, please load first")
}
s.user = pkg.User
if pkg.Role == nil {
return fmt.Errorf("dependence role service is nil, please load first")
}
s.role = pkg.Role
db := conf.C().Mongo.GetDB()
col := db.Collection("policy")
indexs := []mongo.IndexModel{
{
Keys: bsonx.Doc{{Key: "create_at", Value: bsonx.Int32(-1)}},
},
}
_, err := col.Indexes().CreateMany(context.Background(), indexs)
if err != nil {
return err
}
s.col = col
s.log = zap.L().Named("Policy")
return nil
}
// HttpEntry todo
func (s *service) HTTPEntry() *http.EntrySet {
return policy.HttpEntry()
}
func init() {
pkg.RegistryService("policy", Service)
}