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

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