pb_study_client.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. pb "auth-server/rpc_idl/dr_study_pb"
  15. )
  16. var StudyClient *Study
  17. func init() {
  18. StudyClient = new(Study)
  19. }
  20. type Study struct {
  21. basicClient pb.BasicClient
  22. studyClient pb.StudyClient
  23. }
  24. func (s *Study) Setup() {
  25. conn, err := grpc.NewClient(common.ServerConfig.Study, grpc.WithTransportCredentials(insecure.NewCredentials()))
  26. if err != nil {
  27. slog.Error("NewClient failed:", err)
  28. panic(err)
  29. }
  30. slog.Info("study conn success", "conn", conn.GetState())
  31. s.basicClient = pb.NewBasicClient(conn)
  32. s.studyClient = pb.NewStudyClient(conn)
  33. }
  34. func (s *Study) GetSoftwareInfo() *pb.SoftwareInfoReply {
  35. slog.Info("[rpc]SoftwareInfo...")
  36. ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
  37. defer cancel()
  38. r, err := s.basicClient.SoftwareInfo(ctx, &emptypb.Empty{})
  39. if err != nil {
  40. slog.Error("[rpc]SoftwareInfo failed", "err", err)
  41. return &pb.SoftwareInfoReply{
  42. Module: "",
  43. Desc: "",
  44. Build: "",
  45. Version: "",
  46. }
  47. }
  48. return r
  49. }
  50. func (s *Study) CreateStudy(ctx context.Context, in *pb.StudyRequest) (*pb.StudyReply, error) {
  51. slog.Info("[rpc]CreateStudy...")
  52. r, err := s.studyClient.CreateStudy(ctx, in)
  53. if err != nil {
  54. slog.Error("[rpc]CreateStudy failed", "err", err)
  55. return r, err
  56. }
  57. return r, nil
  58. }
  59. func (s *Study) GetStudy(ctx context.Context, id *string) (*pb.StudyReply, error) {
  60. slog.Info("[rpc]GetStudy...")
  61. r, err := s.studyClient.GetStudy(ctx, &pb.IDRequest{
  62. InstanceId: id,
  63. })
  64. if err != nil {
  65. slog.Error("[rpc]GetStudy failed", "err", err)
  66. return r, err
  67. }
  68. return r, nil
  69. }
  70. func (s *Study) CreateImage(ctx context.Context, in *pb.CreateImageRequest) (*pb.CreateImageReply, error) {
  71. slog.Info("[rpc]CreateImage...")
  72. r, err := s.studyClient.CreateImage(ctx, in)
  73. if err != nil {
  74. slog.Error("[rpc]CreateImage failed", "err", err)
  75. return r, err
  76. }
  77. return r, nil
  78. }
  79. func (s *Study) CopyImage(ctx context.Context, in *pb.IDRequest) (*pb.CreateImageReply, error) {
  80. slog.Info("[rpc]CopyImage...")
  81. r, err := s.studyClient.CopyImage(ctx, in)
  82. if err != nil {
  83. slog.Error("[rpc]CopyImage failed", "err", err)
  84. return r, err
  85. }
  86. return r, nil
  87. }
  88. func (s *Study) DeleteImage(ctx context.Context, id *string) (*emptypb.Empty, error) {
  89. slog.Info("[rpc]DeleteImage...")
  90. r, err := s.studyClient.DeleteImage(ctx, &pb.IDRequest{
  91. InstanceId: id,
  92. })
  93. if err != nil {
  94. slog.Error("[rpc]DeleteImage failed", "err", err)
  95. return r, err
  96. }
  97. return r, nil
  98. }