pb_resource.go 1.5 KB

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