Class: Mxrb::Http::Server

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

Overview

Small embedded-Puma adapter for MXRB's loopback-only HTTP services.

Constant Summary collapse

DEFAULT_MAX_THREADS =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, logger: nil, max_threads: DEFAULT_MAX_THREADS, &dispatcher) ⇒ Server

Returns a new instance of Server.

Raises:

  • (ArgumentError)


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

def initialize(host:, port:, logger: nil, max_threads: DEFAULT_MAX_THREADS, &dispatcher)
  raise ArgumentError, 'HTTP dispatcher is required' unless dispatcher

  @host = host.to_s
  @port = Integer(port)
  @dispatcher = dispatcher
  @logger = logger
  @max_threads = Integer(max_threads)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



43
44
45
# File 'lib/mxrb/http/server.rb', line 43

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



43
44
45
# File 'lib/mxrb/http/server.rb', line 43

def port
  @port
end

#pumaObject (readonly)

Returns the value of attribute puma.



43
44
45
# File 'lib/mxrb/http/server.rb', line 43

def puma
  @puma
end

Instance Method Details

#call(environment) ⇒ Object



65
66
67
68
69
70
# File 'lib/mxrb/http/server.rb', line 65

def call(environment)
  request = request_from(environment)
  response = Response.new
  @dispatcher.call(request, response)
  [response.status, response.headers, [response.body.to_s]]
end

#shutdownObject



63
# File 'lib/mxrb/http/server.rb', line 63

def shutdown = @puma&.stop

#start {|@puma| ... } ⇒ Object

Yields:



55
56
57
58
59
60
61
# File 'lib/mxrb/http/server.rb', line 55

def start
  @puma = Puma::Server.new(method(:call), nil, puma_options)
  @puma.add_tcp_listener(host, port)
  yield(@puma) if block_given?
  @thread = @puma.run
  @thread.join
end