module Rad class RackAdapter SERVERS = %w{thin mongrel webrick} # class Callback # def initialize app; end # def self.call env; Rad::Adapter.call env end # end class << self inject( logger: :logger, config: :config ) 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 def parse_config app, options = Rack::Builder.parse_file 'config.ru' app end 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 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 protected def detect_rack_handler return Rack::Handler.get('thin') SERVERS.each do |server_name| begin return Rack::Handler.get(server_name.downcase) rescue LoadError rescue NameError end end fail " Server handler (#{SERVERS.join(', ')}) not found." end end end end