Class: Protein::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/protein/server.rb

Class Method Summary collapse

Class Method Details

.config(config = nil) ⇒ Object



19
20
21
22
# File 'lib/protein/server.rb', line 19

def config(config = nil)
  @config = (@config || {}).merge(config) if config
  @config || {}
end

.route(router) ⇒ Object



6
7
8
# File 'lib/protein/server.rb', line 6

def route(router)
  @router = Router.define(router)
end

.routerObject



15
16
17
# File 'lib/protein/server.rb', line 15

def router
  GetConst.call(@router)
end

.service(service) ⇒ Object



10
11
12
13
# File 'lib/protein/server.rb', line 10

def service(service)
  @router ||= Class.new(Router)
  @router.service(service)
end

.startObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/protein/server.rb', line 32

def start
  worker_count = config.fetch(:concurrency, 5)
  on_worker_boot = config[:on_worker_boot]

  if worker_count.is_a?(Integer) && worker_count > 1
    Parallel.each(1..worker_count, in_processes: worker_count) do |worker|
      Protein.logger.info "Starting server #{worker}/#{worker_count} with PID #{Process.pid}"
      on_worker_boot.call if on_worker_boot.respond_to?(:call)
      transport_class.serve(router)
    end
  else
    transport_class.serve(router)
  end
end

.transport(transport, opts = {}) ⇒ Object



24
25
26
# File 'lib/protein/server.rb', line 24

def transport(transport, opts = {})
  @transport_class = Transport.define(transport, opts)
end

.transport_classObject



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

def transport_class
  GetConst.call(@transport_class)
end