예제 #1
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
}
예제 #2
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
}
예제 #3
0
// New - Will create new Logging instance
func New(opts options.Options) Logging {

	logger := Logging{
		Logger:  logrus.New(),
		Options: opts,
	}

	if formatter, ok := opts.Get("formatter"); ok {
		switch formatter.String() {
		case "text":
			logger.Formatter = new(logrus.TextFormatter)
		case "json":
			logger.Formatter = new(logrus.JSONFormatter)
		default:
			logger.Formatter = new(logrus.TextFormatter)
		}
	}

	if level, ok := opts.Get("level"); ok {
		logger.Level = level.Interface().(logrus.Level)
	}

	return logger
}
예제 #4
0
// NewService -
func NewService(opts options.Options, logger *logging.Entry) (*Service, error) {

	serv, err := services.New(opts)

	if err != nil {
		return nil, err
	}

	_, ok := opts.Get("slack-team-name")

	if !ok {
		return nil, errors.New("You need to provide `slack-team-name` in order to start service...")
	}

	slackToken, ok := opts.Get("slack-token")

	if !ok {
		return nil, errors.New("You need to provide `slack-token` in order to start service...")
	}

	slackDebug, ok := opts.Get("slack-api-debug")

	if !ok {
		return nil, errors.New("You need to provide `slack-api-debug` in order to start service...")
	}

	grpcServer, err := servers.NewGrpcServer(serv, opts, logger)

	if err != nil {
		return nil, err
	}

	restServer, err := servers.NewGrpcRestServer(serv, opts, logger)

	if err != nil {
		return nil, err
	}

	httpServer, err := servers.NewHTTPServer(serv, opts, logger)

	if err != nil {
		return nil, err
	}

	serviceManager, err := managers.New(serv, opts, logger)

	if err != nil {
		return nil, err
	}

	// We are about to attach GRPC service now ...
	if err := serviceManager.Attach("grpc", grpcServer); err != nil {
		return nil, err
	}

	// We are about to attach HTTP service now ...
	if err := serviceManager.Attach("rest", restServer); err != nil {
		return nil, err
	}

	// We are about to attach HTTP service now ...
	if err := serviceManager.Attach("http", httpServer); err != nil {
		return nil, err
	}

	sc := &Service{
		Options:   opts,
		Servicer:  serv,
		Grpc:      grpcServer,
		HTTP:      httpServer,
		REST:      restServer,
		Entry:     logger,
		Managerer: serviceManager,
		Slack:     NewSlack(slackToken.String(), slackDebug.Bool()),
	}

	pb.RegisterSlackServer(sc.GrpcServer(), sc)

	return sc, nil
}
예제 #5
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
}