123456789_123456789_123456789_123456789_123456789_

Module: Rackup::Handler

Relationships & Source Files
Namespace Children
Classes:
Defined in: lib/rackup/handler.rb,
lib/rackup/handler/cgi.rb,
lib/rackup/handler/webrick.rb

Overview

Handlers connect web servers with Rack.

::Rackup includes Handlers for WEBrick and CGI.

Handlers usually are activated by calling MyHandler.run(myapp). A second optional hash can be passed to include server-specific configuration.

Constant Summary

Class Method Summary

Class Method Details

.[](name)

[ GitHub ]

  
# File 'lib/rackup/handler.rb', line 30

def self.[](name)
  name = name.to_sym

  begin
    @handlers[name] || self.const_get(name, false)
  rescue NameError
    # Ignore.
  end
end

.default

[ GitHub ]

  
# File 'lib/rackup/handler.rb', line 84

def self.default
  if rack_handler = ENV[RACKUP_HANDLER]
    self.get(rack_handler)
  elsif rack_handler = ENV[RACK_HANDLER]
    warn "RACK_HANDLER is deprecated, use RACKUP_HANDLER."
    self.get(rack_handler)
  else
    pick SERVER_NAMES
  end
end

.get(name)

[ GitHub ]

  
# File 'lib/rackup/handler.rb', line 40

def self.get(name)
  return nil unless name

  name = name.to_sym

  if server = self[name]
    return server
  end

  begin
    require_handler("rackup/handler", name)
  rescue LoadError
    require_handler("rack/handler", name)
  end

  return self[name]
end

.pick(server_names)

Select first available Rack handler given an Array of server names. Raises LoadError if no handler was found.

> pick ['puma', 'webrick']
#=> Rackup::Handler::WEBrick

Raises:

  • (LoadError)
[ GitHub ]

  
# File 'lib/rackup/handler.rb', line 69

def self.pick(server_names)
  server_names = Array(server_names)

  server_names.each do |server_name|
    begin
      server = self.get(server_name)
     return server if server
    rescue LoadError
      # Ignore.
    end
  end

  raise LoadError, "Couldn't find handler for: #{server_names.join(', ')}."
end

.register(name, klass)

Register a named handler class.

[ GitHub ]

  
# File 'lib/rackup/handler.rb', line 18

def self.register(name, klass)
  if klass.is_a?(String)
    warn "Calling Rackup::Handler.register with a string is deprecated, use the class/module itself.", uplevel: 1

    klass = self.const_get(klass, false)
  end

  name = name.to_sym

  @handlers[name] = klass
end

.require_handler(prefix, const_name)

Transforms server-name constants to their canonical form as filenames, then tries to require them but silences the LoadError if not found

Naming convention:

Foo # => 'foo'
FooBar # => 'foo_bar.rb'
FooBAR # => 'foobar.rb'
FOObar # => 'foobar.rb'
FOOBAR # => 'foobar.rb'
FooBarBaz # => 'foo_bar_baz.rb'
[ GitHub ]

  
# File 'lib/rackup/handler.rb', line 106

def self.require_handler(prefix, const_name)
  file = const_name.to_s.gsub(/^[A-Z]+/) { |pre| pre.downcase }.
    gsub(/[A-Z]+[^A-Z]/, '_\&').downcase

  require(::File.join(prefix, file))
end