server.go 1003 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package grpc
  2. import (
  3. "context"
  4. "flag"
  5. "time"
  6. v1 "go-common/app/service/bbq/recsys-recall/api/grpc/v1"
  7. "go-common/app/service/bbq/recsys-recall/service"
  8. "go-common/library/net/rpc/warden"
  9. xtime "go-common/library/time"
  10. "google.golang.org/grpc"
  11. )
  12. var (
  13. _gRPCAddr string
  14. )
  15. func init() {
  16. flag.StringVar(&_gRPCAddr, "grpc_addr", "0.0.0.0:9000", "default config path")
  17. }
  18. //New 生成rpc服务
  19. func New(srv *service.Service) *warden.Server {
  20. servConf := &warden.ServerConfig{
  21. Addr: _gRPCAddr,
  22. Timeout: xtime.Duration(2 * time.Second),
  23. }
  24. s := warden.NewServer(servConf)
  25. s.Use(middleware())
  26. v1.RegisterRecsysRecallServer(s.Server(), srv)
  27. _, err := s.Start()
  28. if err != nil {
  29. panic("run server failed!" + err.Error())
  30. }
  31. return s
  32. }
  33. func middleware() grpc.UnaryServerInterceptor {
  34. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  35. //call chain
  36. resp, err = handler(ctx, req)
  37. return
  38. }
  39. }