github.com/infraboard/keyauth@v0.8.1/apps/application/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/application"
    13  	"github.com/infraboard/keyauth/conf"
    14  )
    15  
    16  var (
    17  	// Service 服务实例
    18  	svr = &userimpl{service: &service{}}
    19  )
    20  
    21  type service struct {
    22  	col *mongo.Collection
    23  }
    24  
    25  func (s *service) Config() error {
    26  	db := conf.C().Mongo.GetDB()
    27  	ac := db.Collection("application")
    28  
    29  	indexs := []mongo.IndexModel{
    30  		{
    31  			Keys: bsonx.Doc{
    32  				{Key: "user_id", Value: bsonx.Int32(-1)},
    33  				{Key: "name", Value: bsonx.Int32(-1)}},
    34  			Options: options.Index().SetUnique(true),
    35  		},
    36  		{
    37  			Keys:    bsonx.Doc{{Key: "client_id", Value: bsonx.Int32(-1)}},
    38  			Options: options.Index().SetUnique(true),
    39  		},
    40  		{
    41  			Keys: bsonx.Doc{{Key: "create_at", Value: bsonx.Int32(-1)}},
    42  		},
    43  	}
    44  
    45  	_, err := ac.Indexes().CreateMany(context.Background(), indexs)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	s.col = ac
    51  	return nil
    52  }
    53  
    54  func (s *service) Name() string {
    55  	return application.AppName
    56  }
    57  
    58  func (s *service) Registry(server *grpc.Server) {
    59  	application.RegisterServiceServer(server, svr)
    60  }
    61  
    62  func init() {
    63  	app.RegistryGrpcApp(svr)
    64  }