pb_study_client.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_study_pb"
  14. )
  15. var StudyClient *Study
  16. func init() {
  17. StudyClient = new(Study)
  18. }
  19. type Study struct {
  20. basicClient pb.BasicClient
  21. studyClient pb.StudyClient
  22. }
  23. func (s *Study) Setup() {
  24. conn, err := grpc.NewClient(common.ServerConfig.Study, grpc.WithTransportCredentials(insecure.NewCredentials()))
  25. if err != nil {
  26. slog.Error("NewClient failed:", err)
  27. panic(err)
  28. }
  29. slog.Info("study conn success", "conn", conn.GetState())
  30. s.basicClient = pb.NewBasicClient(conn)
  31. s.studyClient = pb.NewStudyClient(conn)
  32. }
  33. func (s *Study) 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 *Study) CreateStudy(ctx context.Context, in *pb.StudyRequest) (*pb.StudyReply, error) {
  50. slog.Info("[rpc]CreateStudy...")
  51. r, err := s.studyClient.CreateStudy(ctx, in)
  52. if err != nil {
  53. slog.Error("[rpc]CreateStudy failed", "err", err)
  54. return r, err
  55. }
  56. return r, nil
  57. }
  58. func (s *Study) GetStudy(ctx context.Context, id *string) (*pb.StudyReply, error) {
  59. slog.Info("[rpc]GetStudy...")
  60. r, err := s.studyClient.GetStudy(ctx, &pb.IDRequest{
  61. InstanceId: id,
  62. })
  63. if err != nil {
  64. slog.Error("[rpc]GetStudy failed", "err", err)
  65. return r, err
  66. }
  67. return r, nil
  68. }