Example #1
0
func (s *Server) ListenGRPC() error {
	grpcPort := s.Config.GetGrpcPortString()
	//Listen
	lis, err := net.Listen("tcp", grpcPort)
	if err != nil {
		return fmt.Errorf("server.go: failed to listen: %v", err)
	}

	var opts []grpc.ServerOption
	if s.Config.TlsCertFile != "" && s.Config.TlsKeyFile != "" {
		creds, err := credentials.NewServerTLSFromFile(s.Config.TlsCertFile, s.Config.TlsKeyFile)
		if err != nil {
			log.Fatalf("server.go: Failed to generate credentials %v", err)
		}
		opts = []grpc.ServerOption{grpc.Creds(creds)}
		s.tlsCheck = tlscheck.New(s.Config.TlsCertFile, s.Config.TlsKeyFile, time.Hour*24*16)
	}

	h := health.New(s, s.Storage)
	grpcServer := grpc.NewServer(opts...)
	catalogGrpc := &catalogGrpcServer{
		server: s,
	}
	pb.RegisterCatalogServiceServer(grpcServer, catalogGrpc)
	grpc_health_v1.RegisterHealthServer(grpcServer, h)

	go http.ListenAndServe(s.Config.GetHealthPortString(), h)

	log.Infof("server.go: Binding %s for grpc", grpcPort)
	//Serve
	return grpcServer.Serve(lis)
}
Example #2
0
func (s *Server) ListenGRPC() error {
	grpcPort := s.Config.GetGrpcPortString()
	//Listen
	lis, err := net.Listen("tcp", grpcPort)
	if err != nil {
		return fmt.Errorf("server.go: failed to listen, %v", err)
	}
	hs := health.New()
	var opts []grpc.ServerOption
	if s.Config.TlsCertFile != "" && s.Config.TlsKeyFile != "" {
		creds, err := credentials.NewServerTLSFromFile(s.Config.TlsCertFile, s.Config.TlsKeyFile)
		if err != nil {
			return fmt.Errorf("server.go: Failed to generate credentials %v", err)
		}
		opts = []grpc.ServerOption{grpc.Creds(creds)}
		s.tlsCheck = tls.New(s.Config.TlsCertFile, s.Config.TlsKeyFile, time.Hour*24*19)
		hs.Checks = append(hs.Checks, s.tlsCheck)
	}
	grpcServer := grpc.NewServer(opts...)

	watchGrpc := &watchGrpcServer{
		server: s,
	}

	pb.RegisterWatchServiceServer(grpcServer, watchGrpc)
	grpc_health_v1.RegisterHealthServer(grpcServer, hs)

	if !s.Config.NoRedis {
		cl, err := NewRedisClient(s.Config)
		if err != nil {
			return fmt.Errorf("failed to create redis client, %v", err)
		}
		s.Redis = cl
		hs.Checks = append(hs.Checks, s.Redis)
	}

	log.Infof("server.go: Binding %s for grpc and :%d for health", grpcPort, s.Config.HealthPort)
	go http.ListenAndServe(s.Config.GetHealthPortString(), hs)
	go h.run()
	return grpcServer.Serve(lis)
}