pb_resource.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package service
  2. import (
  3. "context"
  4. "log/slog"
  5. )
  6. import (
  7. "google.golang.org/grpc"
  8. "google.golang.org/grpc/credentials/insecure"
  9. )
  10. import (
  11. "auth-server/common"
  12. pb "auth-server/rpc_idl/dr_resource_pb"
  13. )
  14. var ResourceService *Resource
  15. func init() {
  16. ResourceService = new(Resource)
  17. }
  18. type Resource struct {
  19. basicClient pb.BasicClient
  20. configClient pb.ConfigClient
  21. }
  22. func (s *Resource) Setup() {
  23. conn, err := grpc.NewClient(common.ServerConfig.Resource, grpc.WithTransportCredentials(insecure.NewCredentials()))
  24. if err != nil {
  25. slog.Error("NewClient failed:", err)
  26. panic(err)
  27. }
  28. slog.Info("resource conn success", "conn", conn.GetState())
  29. s.basicClient = pb.NewBasicClient(conn)
  30. s.configClient = pb.NewConfigClient(conn)
  31. }
  32. func (s *Resource) GetSoftwareInfo() *pb.SoftwareInfoReply {
  33. slog.Info("[rpc]SoftwareInfo...")
  34. r, err := s.basicClient.SoftwareInfo(context.Background(), &pb.EmptyRequest{})
  35. if err != nil {
  36. slog.Error("[rpc]SoftwareInfo failed:", err)
  37. return &pb.SoftwareInfoReply{
  38. Module: "",
  39. Desc: "",
  40. Build: "",
  41. Version: "",
  42. }
  43. }
  44. return r
  45. }
  46. func (s *Resource) GetConfigOptionList(ctx context.Context, flag string, enable bool) ([]*pb.ConfigOption, error) {
  47. slog.Info("[rpc]GetConfigOptionList...")
  48. r, err := s.configClient.ConfigOptionList(ctx, &pb.ConfigOptionListRequest{
  49. Flag: flag,
  50. IsEnabled: enable,
  51. })
  52. if err != nil {
  53. slog.Error("[rpc]GetConfigOptionList failed:", err)
  54. }
  55. return r.GetConfigOption(), err
  56. }