server.go 707 B

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