github.com/infraboard/keyauth@v0.8.1/apps/domain/impl/impl.go (about)

     1  package impl
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/infraboard/mcube/app"
     7  	"go.mongodb.org/mongo-driver/mongo"
     8  	"go.mongodb.org/mongo-driver/mongo/options"
     9  	"go.mongodb.org/mongo-driver/x/bsonx"
    10  	"google.golang.org/grpc"
    11  
    12  	"github.com/infraboard/keyauth/apps/domain"
    13  	"github.com/infraboard/keyauth/conf"
    14  )
    15  
    16  var (
    17  	// Service 服务实例
    18  	svr = &service{}
    19  )
    20  
    21  type service struct {
    22  	col           *mongo.Collection
    23  	enableCache   bool
    24  	notifyCachPre string
    25  	domain.UnimplementedServiceServer
    26  }
    27  
    28  func (s *service) Config() error {
    29  	db := conf.C().Mongo.GetDB()
    30  	dc := db.Collection("domain")
    31  
    32  	indexs := []mongo.IndexModel{
    33  		{
    34  			Keys:    bsonx.Doc{{Key: "name", Value: bsonx.Int32(-1)}},
    35  			Options: options.Index().SetUnique(true),
    36  		},
    37  		{
    38  			Keys:    bsonx.Doc{{Key: "ldap_config.base_dn", Value: bsonx.Int32(-1)}},
    39  			Options: options.Index().SetUnique(true),
    40  		},
    41  		{
    42  			Keys: bsonx.Doc{{Key: "create_at", Value: bsonx.Int32(-1)}},
    43  		},
    44  	}
    45  
    46  	_, err := dc.Indexes().CreateMany(context.Background(), indexs)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	s.col = dc
    52  
    53  	return nil
    54  }
    55  
    56  func (s *service) Name() string {
    57  	return domain.AppName
    58  }
    59  
    60  func (s *service) Registry(server *grpc.Server) {
    61  	domain.RegisterServiceServer(server, svr)
    62  }
    63  
    64  func init() {
    65  	app.RegistryGrpcApp(svr)
    66  }