pb_study_client.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. r, err := s.studyClient.CreateStudy(ctx, in)
  51. if err != nil {
  52. slog.Error("[rpc]CreateStudy failed", "err", err)
  53. return r, err
  54. }
  55. return r, nil
  56. }