pb_resource.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. )
  11. import (
  12. "auth-server/common"
  13. pb "auth-server/rpc_idl/dr_resource_pb"
  14. )
  15. var ResourceService *Resource
  16. func init() {
  17. ResourceService = new(Resource)
  18. }
  19. type Resource struct {
  20. basicClient pb.BasicClient
  21. configClient pb.ConfigClient
  22. }
  23. func (s *Resource) Setup() {
  24. conn, err := grpc.NewClient(common.ServerConfig.Resource, grpc.WithTransportCredentials(insecure.NewCredentials()))
  25. if err != nil {
  26. slog.Error("NewClient failed:", err)
  27. panic(err)
  28. }
  29. slog.Info("resource conn success", "conn", conn.GetState())
  30. s.basicClient = pb.NewBasicClient(conn)
  31. s.configClient = pb.NewConfigClient(conn)
  32. }
  33. func (s *Resource) GetSoftwareInfo() *pb.SoftwareInfoReply {
  34. slog.Info("[rpc]SoftwareInfo...")
  35. ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
  36. defer cancel()
  37. r, err := s.basicClient.SoftwareInfo(ctx, &pb.EmptyRequest{})
  38. if err != nil {
  39. slog.Error("[rpc]SoftwareInfo failed", "err", err)
  40. return &pb.SoftwareInfoReply{
  41. Module: "",
  42. Desc: "",
  43. Build: "",
  44. Version: "",
  45. }
  46. }
  47. return r
  48. }
  49. func (s *Resource) GetConfigOptionList(ctx context.Context, flag string, enable bool) ([]*pb.ConfigOption, error) {
  50. slog.Info("[rpc]GetConfigOptionList...")
  51. r, err := s.configClient.ConfigOptionList(ctx, &pb.ConfigOptionListRequest{
  52. Flag: flag,
  53. IsEnabled: enable,
  54. })
  55. if err != nil {
  56. slog.Error("[rpc]GetConfigOptionList failed", "err", err)
  57. }
  58. return r.GetConfigOption(), err
  59. }