server.go 900 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package grpc
  2. import (
  3. "context"
  4. "go-common/app/service/main/share/api"
  5. "go-common/app/service/main/share/model"
  6. "go-common/app/service/main/share/service"
  7. "go-common/library/net/rpc/warden"
  8. )
  9. // server .
  10. type server struct {
  11. srv *service.Service
  12. }
  13. // New share warden rpc server.
  14. func New(cfg *warden.ServerConfig, srv *service.Service) *warden.Server {
  15. w := warden.NewServer(cfg)
  16. v1.RegisterShareServer(w.Server(), &server{srv: srv})
  17. var err error
  18. w, err = w.Start()
  19. if err != nil {
  20. panic(err)
  21. }
  22. return w
  23. }
  24. // AddShare .
  25. func (s *server) AddShare(ctx context.Context, req *v1.AddShareRequest) (*v1.AddShareReply, error) {
  26. p := &model.ShareParams{
  27. OID: req.Oid,
  28. MID: req.Mid,
  29. TP: int(req.Type),
  30. IP: req.Ip,
  31. }
  32. shares, err := s.srv.Add(ctx, p)
  33. if err != nil {
  34. return nil, err
  35. }
  36. addShareReply := &v1.AddShareReply{
  37. Shares: shares,
  38. }
  39. return addShareReply, nil
  40. }