Class: Rad::RackAdapter

Inherits:
Object show all
Defined in:
lib/rad/http/support/rack/rack_adapter.rb

Constant Summary collapse

SERVERS =
%w{thin mongrel webrick}

Class Method Summary collapse

Class Method Details

.initialize_rack(builder = nil, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rad/http/support/rack/rack_adapter.rb', line 16

def initialize_rack builder = nil, &block
  @rack_configurations ||= []
  if block
    raise "provide configuration block!" unless block
    @rack_configurations << block
  else
    if @rack_configurations.empty?
      raise "Common App not defined! Use profiles (like rad/profiles/web), define it by yourself or use your own configuration!"
    end
    @rack_configurations.each{|conf| conf.call builder}
  end
end

.parse_configObject



29
30
31
32
# File 'lib/rad/http/support/rack/rack_adapter.rb', line 29

def parse_config
  app, options = Rack::Builder.parse_file 'config.ru'
  app
end

.quit!(server, handler_name) ⇒ Object



46
47
48
49
50
# File 'lib/rad/http/support/rack/rack_adapter.rb', line 46

def quit!(server, handler_name)
  ## Use thins' hard #stop! if available, otherwise just #stop
  server.respond_to?(:stop!) ? server.stop! : server.stop
  puts "\nRad stopped" unless handler_name =~/cgi/i        
end

.run(app, host, port) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rad/http/support/rack/rack_adapter.rb', line 34

def run app, host, port
  handler = detect_rack_handler
  handler_name = handler.name.gsub(/.*::/, '')
  logger.info "  HTTP server started on #{host}:#{port}" unless config.test?
  # unless handler_name =~/cgi/i
  handler.run app, Host: host, Port: port do |server|
    [:INT, :TERM].each {|sig| trap(sig){quit!(server, handler_name)}}
  end
rescue Errno::EADDRINUSE => e
  logger.error "Port #{port} taken!"
end