#!/usr/bin/env ruby # -*- ruby -*- require 'optparse' automatic = false server = nil env = "development" options = {:Port => 9292, :Host => "0.0.0.0", :AccessLog => []} opts = OptionParser.new("", 24, ' ') { |opts| opts.banner = "Usage: rackup [ruby options] [rack options] [rackup config]" opts.separator "" opts.separator "Ruby options:" lineno = 1 opts.on("-e", "--eval LINE", "evaluate a LINE of code") { |line| eval line, TOPLEVEL_BINDING, "-e", lineno lineno += 1 } opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") { $DEBUG = true } opts.on("-w", "--warn", "turn warnings on for your script") { $-w = true } opts.on("-I", "--include PATH", "specify $LOAD_PATH (may be used more than once)") { |path| $LOAD_PATH.unshift(*path.split(":")) } opts.on("-r", "--require LIBRARY", "require the library, before executing your script") { |library| require library } opts.separator "" opts.separator "Rack options:" opts.on("-s", "--server SERVER", "serve using SERVER (webrick/mongrel)") { |s| server = s } opts.on("-o", "--host HOST", "listen on HOST (default: 0.0.0.0)") { |host| options[:Host] = host } opts.on("-p", "--port PORT", "use PORT (default: 9292)") { |port| options[:Port] = port } opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e| env = e } opts.separator "" opts.separator "Common options:" opts.on_tail("-h", "--help", "Show this message") do puts opts exit end opts.on_tail("--version", "Show version") do require 'rack' puts "Rack #{Rack.version}" exit end opts.parse! ARGV } require 'pp' if $DEBUG config = ARGV[0] || "config.ru" if !File.exist? config abort "configuration #{config} not found" end if config =~ /\.ru$/ cfgfile = File.read(config) if cfgfile[/^#\\(.*)/] opts.parse! $1.split(/\s+/) end require 'rack' inner_app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app", nil, config else require 'rack' require config inner_app = Object.const_get(File.basename(config, '.rb').capitalize) end case server when nil # Guess. if ENV.include?("PHP_FCGI_CHILDREN") server = Rack::Handler::FastCGI elsif ENV.include?("REQUEST_METHOD") server = Rack::Handler::CGI else begin server = Rack::Handler::Mongrel rescue LoadError => e server = Rack::Handler::WEBrick end end when "mongrel" server = Rack::Handler::Mongrel when "webrick" server = Rack::Handler::WEBrick when "cgi" server = Rack::Handler::CGI when "fastcgi" server = Rack::Handler::FastCGI else server = Rack::Handler.const_get(server.capitalize) end p server if $DEBUG case env when "development" app = Rack::Builder.new { use Rack::CommonLogger, STDERR unless server.name =~ /CGI/ use Rack::ShowExceptions use Rack::Reloader use Rack::Lint run inner_app }.to_app when "deployment" app = Rack::Builder.new { use Rack::CommonLogger, STDERR unless server.name =~ /CGI/ run inner_app }.to_app when "none" app = inner_app end if $DEBUG pp app pp inner_app end server.run app, options