Class: Crystal::RackAdapter

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

Constant Summary collapse

SERVERS =
%w{mongrel thin webrick}

Class Method Summary collapse

Class Method Details

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



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

def build_common_app builder = nil, &block
  if block
    @build_common_app = block
  else
    unless @build_common_app
      raise "Common App not defined! Use profiles (like crystal/profiles/web), define it by yourself or use your own configuration!"
    end
    @build_common_app.call builder
  end
end

.parse_configObject



27
28
29
30
# File 'lib/crystal/http/support/rack/rack_adapter.rb', line 27

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

.quit!(server, handler_name) ⇒ Object



44
45
46
47
48
# File 'lib/crystal/http/support/rack/rack_adapter.rb', line 44

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

.run(app) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/crystal/http/support/rack/rack_adapter.rb', line 32

def run app
  handler = detect_rack_handler
  handler_name = handler.name.gsub(/.*::/, '')
  # unless handler_name =~/cgi/i
  logger.info "Crystal #{Crystal::VERSION} started on #{config.port!} in :#{config.environment!} mode (at #{Time.now.to_s})\n\n"
  handler.run app, :Host => config.host!, :Port => config.port! do |server|
    [:INT, :TERM].each {|sig| trap(sig){quit!(server, handler_name)}}
  end
rescue Errno::EADDRINUSE => e
  logger.error "Port #{config.port!} taken!"
end