示例#1
0
文件: grpc.go 项目: golanghr/platform
// NewGrpcServer -
func NewGrpcServer(serv services.Servicer, opts options.Options, logger *logging.Entry) (Serverer, error) {

	addr, addrOk := opts.Get("grpc-addr")

	if !addrOk {
		return nil, errors.New("You must provide `grpc-addr` in order to create gRPC server...")
	}

	listener, err := net.Listen("tcp", addr.String())

	if err != nil {
		return nil, fmt.Errorf("Failed to listen: %v", err)
	}

	var grpcOpts []grpc.ServerOption

	if useGrpc, ok := opts.Get("grpc-tls"); ok && useGrpc.Bool() {
		certFile, certOk := opts.Get("grpc-tls-cert")
		certKey, keyOk := opts.Get("grpc-tls-key")

		if !certOk {
			return nil, errors.New("You must provide `grpc-tls-cert` in order to create gRPC server...")
		}

		if !keyOk {
			return nil, errors.New("You must provide `grpc-tls-key` in order to create gRPC server...")
		}

		creds, err := credentials.NewServerTLSFromFile(certFile.String(), certKey.String())

		if err != nil {
			return nil, fmt.Errorf("Failed to generate gRPC credentials: %v", err)
		}

		grpcOpts = []grpc.ServerOption{grpc.Creds(creds)}
	}

	if maxStreams, msOk := opts.Get("grpc-max-concurrent-streams"); msOk {
		grpcOpts = []grpc.ServerOption{grpc.MaxConcurrentStreams(maxStreams.UInt32())}
	}

	grpcServer := grpc.NewServer(grpcOpts...)

	s := &Grpc{
		Options:           opts,
		Server:            grpcServer,
		Listener:          listener,
		Entry:             logger,
		Servicer:          serv,
		ConnectivityState: &ConnectivityState{},
		Interrupt:         serv.GetInterruptChan(),
	}

	if listenForever, lfOk := opts.Get("grpc-listen-forever"); lfOk {
		s.ListenForever = listenForever.Bool()
	}

	return Serverer(s), nil
}
示例#2
0
// New -
func New(serv services.Servicer, opts options.Options, logger *logging.Entry) (Managerer, error) {
	waitTimeout := DefaultInterruptWaitTimeout

	if miwt, ok := opts.Get("manager-interrupt-wait-timeout"); ok {
		waitTimeout = miwt.Int()
	}

	return Managerer(&Manager{
		Servicer:             serv,
		Options:              opts,
		Entry:                logger,
		Servers:              make(map[string]servers.Serverer),
		InterruptWaitTimeout: time.Duration(waitTimeout) * time.Second,
		Interrupt:            serv.GetInterruptChan(),
	}), nil
}
示例#3
0
文件: http.go 项目: golanghr/platform
// NewHTTPServer -
func NewHTTPServer(serv services.Servicer, opts options.Options, logger *logging.Entry) (Serverer, error) {

	addr, addrOk := opts.Get("http-addr")

	if !addrOk {
		return nil, errors.New("You must provide `http-addr` in order to create HTTP server...")
	}

	certFile, certOk := opts.Get("http-tls-cert")
	certKeyFile, keyOk := opts.Get("http-tls-key")

	s := &HTTP{
		Server:            &http.Server{Addr: addr.String()},
		Options:           opts,
		Entry:             logger,
		Servicer:          serv,
		ConnectivityState: &ConnectivityState{},
		Interrupt:         serv.GetInterruptChan(),
	}

	s.Server.SetKeepAlivesEnabled(true)

	if useTLS, ok := opts.Get("http-tls"); ok && useTLS.Bool() {
		if !certOk {
			return nil, errors.New("You must provide `http-tls-cert` in order to create HTTP server...")
		}

		if !keyOk {
			return nil, errors.New("You must provide `http-tls-key` in order to create HTTP server...")
		}

		s.TLSCertFile = certFile.String()
		s.TLSKeyFile = certKeyFile.String()
		s.UseTLS = useTLS.Bool()
	}

	if listenForever, lfOk := opts.Get("http-listen-forever"); lfOk {
		s.ListenForever = listenForever.Bool()
	}

	return Serverer(s), nil
}
示例#4
0
// NewHttpGrpcHandler - Will return back new HTTP GRPC handler
func NewHttpGrpcHandler(serv services.Servicer, logger *logging.Entry, handler func(context.Context, *runtime.ServeMux, *grpc.ClientConn) error) (*HttpGrpcHandler, error) {
	grpcAddr, ok := serv.GetOptions().Get("grpc-addr")

	if !ok {
		return nil, errors.New("In order to register new grpc http handler `grpc-addr` must be set.")
	}

	certFile, ok := serv.GetOptions().Get("grpc-tls-cert")

	if !ok {
		return nil, errors.New("In order to register new grpc http handler `grpc-tls-cert` must be set.")
	}

	certDomain, ok := serv.GetOptions().Get("grpc-tls-domain")
	if !ok {
		return nil, errors.New("In order to register new grpc http handler `grpc-tls-domain` must be set.")
	}

	httpgrpc := &HttpGrpcHandler{
		ctx:        context.Background(),
		ServeMux:   runtime.NewServeMux(),
		Servicer:   serv,
		Entry:      logger,
		GrpcAddr:   grpcAddr,
		CertFile:   certFile,
		CertDomain: certDomain,
	}

	// Will register provider handler (protocol buffer client) and start grpc.Dial
	if err := httpgrpc.RegisterHandlerTLS(handler); err != nil {
		return nil, err
	}

	return httpgrpc, nil
}