Class: Gruf::Server
Overview
Represents a gRPC server. Automatically loads and augments gRPC handlers and services based on configuration values.
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Hash of options passed into the server.
-
#port ⇒ Integer
readonly
The port the server is bound to.
Instance Method Summary collapse
-
#add_interceptor(klass, opts = {}) ⇒ Object
Add an interceptor to the server.
- #add_service(klass) ⇒ Object
-
#initialize(options = {}) ⇒ Server
constructor
Initialize the server and load and setup the services.
-
#server ⇒ GRPC::RpcServer
The GRPC server running.
-
#start! ⇒ Object
Start the gRPC server.
Methods included from Loggable
Constructor Details
#initialize(options = {}) ⇒ Server
Initialize the server and load and setup the services
34 35 36 37 38 39 40 |
# File 'lib/gruf/server.rb', line 34 def initialize( = {}) @options = || {} @interceptors = .fetch(:interceptor_registry, Gruf.interceptors) @interceptors = Gruf::Interceptors::Registry.new unless @interceptors.is_a?(Gruf::Interceptors::Registry) @services = [] setup! end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns Hash of options passed into the server.
27 28 29 |
# File 'lib/gruf/server.rb', line 27 def @options end |
#port ⇒ Integer (readonly)
Returns 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
82 83 84 |
# File 'lib/gruf/server.rb', line 82 def add_interceptor(klass, opts = {}) @interceptors.use(klass, opts) end |
#add_service(klass) ⇒ Object
72 73 74 |
# File 'lib/gruf/server.rb', line 72 def add_service(klass) @services << klass unless @services.include?(klass) end |
#server ⇒ GRPC::RpcServer
Returns 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() @port = @server.add_http2_port(.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 |