Class: Gruf::Server

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/gruf/server.rb

Overview

Represents a gRPC server. Automatically loads and augments gRPC handlers and services based on configuration values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(options = {}) ⇒ Server

Initialize the server and load and setup the services

Parameters:

  • options (Hash) (defaults to: {})


37
38
39
40
41
42
43
# File 'lib/gruf/server.rb', line 37

def initialize(options = {})
  @options = options || {}
  @interceptors = options.fetch(:interceptor_registry, Gruf.interceptors)
  @interceptors = Gruf::Interceptors::Registry.new unless @interceptors.is_a?(Gruf::Interceptors::Registry)
  @services = []
  setup!
end

Instance Attribute Details

#optionsHash (readonly)

Returns Hash of options passed into the server.

Returns:

  • (Hash)

    Hash of options passed into the server



30
31
32
# File 'lib/gruf/server.rb', line 30

def options
  @options
end

#portInteger (readonly)

Returns The port the server is bound to.

Returns:

  • (Integer)

    The port the server is bound to



28
29
30
# File 'lib/gruf/server.rb', line 28

def port
  @port
end

#serverGRPC::RpcServer (readonly)

Returns The GRPC server running.

Returns:

  • (GRPC::RpcServer)

    The GRPC server running



26
27
28
# File 'lib/gruf/server.rb', line 26

def server
  @server
end

Instance Method Details

#add_interceptor(klass, opts = {}) ⇒ Object

Add an interceptor to the server

Parameters:



81
82
83
# File 'lib/gruf/server.rb', line 81

def add_interceptor(klass, opts = {})
  @interceptors.use(klass, opts)
end

#add_service(klass) ⇒ Object

Parameters:

  • klass (Class)


71
72
73
# File 'lib/gruf/server.rb', line 71

def add_service(klass)
  @services << klass unless @services.include?(klass)
end

#start!Object

Start the gRPC server

:nocov:



61
62
63
64
65
# File 'lib/gruf/server.rb', line 61

def start!
  logger.info { 'Booting gRPC Server...' }
  server.run_till_terminated
  logger.info { 'Shutting down gRPC server...' }
end