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

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