server.go 953 B

12345678910111213141516171819202122232425262728293031323334
  1. package grpc
  2. import (
  3. "context"
  4. rpc "go-common/app/service/openplatform/ticket-sales/api/grpc/v1"
  5. "go-common/app/service/openplatform/ticket-sales/service"
  6. "go-common/library/net/rpc/warden"
  7. "go-common/app/service/openplatform/ticket-sales/service/mis"
  8. "google.golang.org/grpc"
  9. )
  10. //New 生成rpc服务
  11. func New(srv *service.Service, misSrv *mis.Mis) *warden.Server {
  12. s := warden.NewServer(nil)
  13. s.Use(middleware())
  14. rpc.RegisterTradeServer(s.Server(), srv)
  15. rpc.RegisterPromotionServer(s.Server(), srv)
  16. rpc.RegisterPromotionMisServer(s.Server(), misSrv)
  17. rpc.RegisterTicketServer(s.Server(), srv)
  18. _, err := s.Start()
  19. if err != nil {
  20. panic("run server failed!" + err.Error())
  21. }
  22. return s
  23. }
  24. func middleware() grpc.UnaryServerInterceptor {
  25. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  26. //call chain
  27. resp, err = handler(ctx, req)
  28. return
  29. }
  30. }