예제 #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
}