Class: Rack::Mux
- Inherits:
-
Object
- Object
- Rack::Mux
- Defined in:
- lib/rack/mux.rb
Defined Under Namespace
Classes: Server
Constant Summary collapse
- VERSION =
'0.1.2'
- HEADER =
'X-Mux-Uri'
Instance Method Summary collapse
- #call(env) ⇒ Object
- #default_options ⇒ Object
- #find_port ⇒ Object
-
#initialize(app, options = {}) ⇒ Mux
constructor
A new instance of Mux.
- #rackup ⇒ Object
- #server_options ⇒ Object
- #wait_for_server ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Mux
Returns a new instance of Mux.
8 9 10 11 12 13 14 15 |
# File 'lib/rack/mux.rb', line 8 def initialize(app, = {}) @app, @rack_options = app, @host = [:Host] || '0.0.0.0' @port = [:Port] || find_port @uri = URI.parse("http://#{@host}:#{@port}") rackup end |
Instance Method Details
#call(env) ⇒ Object
17 18 19 |
# File 'lib/rack/mux.rb', line 17 def call(env) @app.call(env.merge(HEADER => @uri.to_s, 'SERVER_PORT' => @port.to_s)) end |
#default_options ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/rack/mux.rb', line 33 def { :environment => ENV['RACK_ENV'] || "development", :Port => @port, :Host => @host } end |
#find_port ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rack/mux.rb', line 41 def find_port loop do port = rand(64510) + 1025 begin TCPSocket.new(@host, port) rescue Errno::ECONNREFUSED return port end end end |
#rackup ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/rack/mux.rb', line 21 def rackup Thread.new() do || Server.start() end wait_for_server end |
#server_options ⇒ Object
29 30 31 |
# File 'lib/rack/mux.rb', line 29 def .merge(@rack_options).merge(:app => self) end |
#wait_for_server ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rack/mux.rb', line 52 def wait_for_server Timeout::timeout(30) do loop do begin return TCPSocket.new(@host, @port) rescue Errno::ECONNREFUSED sleep 1 rescue => e raise Error, e. end end end end |