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: {})


34
35
36
37
38
39
40
# File 'lib/gruf/server.rb', line 34

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



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

def options
  @options
end

#portInteger (readonly)

Returns The port the server is bound to.

Returns:

  • (Integer)

    The port the server is bound to



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

def port
  @port
end

Instance Method Details

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

Add an interceptor to the server

Parameters:



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

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

#add_service(klass) ⇒ Object

Parameters:

  • klass (Class)


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

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

#serverGRPC::RpcServer

Returns The GRPC server running.

Returns:

  • (GRPC::RpcServer)

    The GRPC server running



45
46
47
48
49
50
51
52
# File 'lib/gruf/server.rb', line 45

def server
  unless @server
    @server = GRPC::RpcServer.new(options)
    @port = @server.add_http2_port(options.fetch(:hostname, Gruf.server_binding_url), ssl_credentials)
    @services.each { |s| @server.handle(s) }
  end
  @server
end

#start!Object

Start the gRPC server

:nocov: rubocop:disable Lint/ShadowedException



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

def start!
  logger.info { 'Booting gRPC Server...' }
  server.run_till_terminated
rescue Interrupt, SignalException, SystemExit
  logger.info { 'Shutting down gRPC server...' }
  server.stop
end