123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package service
- import (
- "context"
- "errors"
- "log/slog"
- "time"
- )
- import (
- "google.golang.org/grpc"
- "google.golang.org/grpc/credentials/insecure"
- )
- import (
- "auth-server/common"
- pb "auth-server/rpc_idl/dr_resource_pb"
- )
- var ResourceClient *Resource
- func init() {
- ResourceClient = new(Resource)
- }
- type Resource struct {
- basicClient pb.BasicClient
- configClient pb.ConfigClient
- }
- func (s *Resource) Setup() {
- conn, err := grpc.NewClient(common.ServerConfig.Resource, grpc.WithTransportCredentials(insecure.NewCredentials()))
- if err != nil {
- slog.Error("NewClient failed:", err)
- panic(err)
- }
- slog.Info("resource conn success", "conn", conn.GetState())
- s.basicClient = pb.NewBasicClient(conn)
- s.configClient = pb.NewConfigClient(conn)
- }
- func (s *Resource) GetSoftwareInfo() *pb.SoftwareInfoReply {
- slog.Info("[rpc]SoftwareInfo...")
- ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
- defer cancel()
- r, err := s.basicClient.SoftwareInfo(ctx, &pb.EmptyRequest{})
- if err != nil {
- slog.Error("[rpc]SoftwareInfo failed", "err", err)
- return &pb.SoftwareInfoReply{
- Module: "",
- Desc: "",
- Build: "",
- Version: "",
- }
- }
- return r
- }
- func (s *Resource) GetOptions(ctx context.Context, group, flag *string) (*pb.OptionReply, error) {
- if group == nil {
- return nil, errors.New("group is nil")
- }
- if flag == nil {
- return nil, errors.New("flag is nil")
- }
- r, err := s.configClient.GetOptions(ctx, &pb.OptionRequest{
- Group: *group,
- Flag: *flag,
- })
- if err != nil {
- slog.Error("[rpc]GetOptions failed", "err", err)
- }
- slog.Info("[rpc]GetOptions result", "rows", len(r.Option))
- return r, err
- }
- func (s *Resource) GetConfigOptionList(ctx context.Context, flag string, enable bool) ([]*pb.ConfigOption, error) {
- slog.Info("[rpc]GetConfigOptionList...")
- r, err := s.configClient.ConfigOptionList(ctx, &pb.ConfigOptionListRequest{
- Flag: flag,
- IsEnabled: enable,
- })
- if err != nil {
- slog.Error("[rpc]GetConfigOptionList failed", "err", err)
- }
- return r.GetConfigOption(), err
- }
|