Class: Mxrb::RubyApp::Server

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

Overview

Loopback HTTP server for the generated backend and browser frontend.

Constant Summary collapse

MAX_BODY_BYTES =
1_048_576
LOOPBACK_HOSTS =
%w[127.0.0.1 ::1 localhost].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, host: '127.0.0.1', port: 9292, logger: nil, environment: nil) ⇒ Server

Returns a new instance of Server.

Raises:

  • (ArgumentError)


989
990
991
992
993
994
995
996
997
# File 'lib/mxrb/ruby_app.rb', line 989

def initialize(root, host: '127.0.0.1', port: 9292, logger: nil, environment: nil)
  @host = host.to_s
  raise ArgumentError, 'the Ruby application server must bind to loopback' unless LOOPBACK_HOSTS.include?(@host)

  @port = Integer(port)
  @application = Application.new(root, environment:)
  @sessions = @application.session_manager
  @logger = logger
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



987
988
989
# File 'lib/mxrb/ruby_app.rb', line 987

def application
  @application
end

#hostObject (readonly)

Returns the value of attribute host.



987
988
989
# File 'lib/mxrb/ruby_app.rb', line 987

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



987
988
989
# File 'lib/mxrb/ruby_app.rb', line 987

def port
  @port
end

Instance Method Details

#shutdownObject



1007
# File 'lib/mxrb/ruby_app.rb', line 1007

def shutdown = @server&.shutdown

#startObject



999
1000
1001
1002
1003
1004
1005
# File 'lib/mxrb/ruby_app.rb', line 999

def start
  @server = Http::Server.new(host:, port:, logger: @logger) { dispatch(_1, _2) }
  application.start_scheduler
  @server.start { yield(_1) if block_given? }
ensure
  application.close
end