pb_resource_client.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package service
  2. import (
  3. "context"
  4. "log/slog"
  5. "time"
  6. )
  7. import (
  8. "google.golang.org/grpc"
  9. "google.golang.org/grpc/credentials/insecure"
  10. "google.golang.org/protobuf/types/known/emptypb"
  11. )
  12. import (
  13. "auth-server/common"
  14. "auth-server/logger"
  15. pb "auth-server/rpc_idl/dr_resource_pb"
  16. )
  17. var ResourceClient *Resource
  18. func init() {
  19. ResourceClient = new(Resource)
  20. }
  21. type Resource struct {
  22. basicClient pb.BasicClient
  23. configClient pb.ConfigClient
  24. }
  25. func (s *Resource) Setup() {
  26. conn, err := grpc.NewClient(common.ServerConfig.Resource,
  27. grpc.WithTransportCredentials(insecure.NewCredentials()),
  28. grpc.WithUnaryInterceptor(logger.GRPCLoggerClientInterceptor(logger.WithGroup("grpc_c[resource]"))),
  29. )
  30. if err != nil {
  31. slog.Error("NewClient failed:", err)
  32. panic(err)
  33. }
  34. slog.Info("resource conn success", "conn", conn.GetState())
  35. s.basicClient = pb.NewBasicClient(conn)
  36. s.configClient = pb.NewConfigClient(conn)
  37. }
  38. func (s *Resource) GetSoftwareInfo() *pb.SoftwareInfoReply {
  39. ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
  40. defer cancel()
  41. r, err := s.basicClient.SoftwareInfo(ctx, &emptypb.Empty{})
  42. if err != nil {
  43. return &pb.SoftwareInfoReply{
  44. Module: "",
  45. Desc: "",
  46. Build: "",
  47. Version: "",
  48. }
  49. }
  50. return r
  51. }
  52. func (s *Resource) GetOptions(ctx context.Context, group, flag *string) (*pb.OptionReply, error) {
  53. if group == nil {
  54. return nil, common.MissingParam.SetParam("Name", "group")
  55. }
  56. if flag == nil {
  57. return nil, common.MissingParam.SetParam("Name", "group")
  58. }
  59. r, err := s.configClient.GetOptions(ctx, &pb.OptionRequest{
  60. Group: *group,
  61. Flag: *flag,
  62. })
  63. return r, err
  64. }
  65. func (s *Resource) GetConfigOptionList(ctx context.Context, flag string, enable bool) ([]*pb.ConfigOption, error) {
  66. r, err := s.configClient.ConfigOptionList(ctx, &pb.ConfigOptionListRequest{
  67. Flag: flag,
  68. IsEnabled: enable,
  69. })
  70. if err != nil {
  71. return nil, err
  72. }
  73. return r.GetConfigOption(), err
  74. }