Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / railties / lib / commands / server.rb
1 require 'active_support'
2 require 'action_controller'
3
4 require 'fileutils'
5 require 'optparse'
6
7 # TODO: Push Thin adapter upstream so we don't need worry about requiring it
8 begin
9 require_library_or_gem 'thin'
10 rescue Exception
11 # Thin not available
12 end
13
14 options = {
15 :Port => 3000,
16 :Host => "0.0.0.0",
17 :environment => (ENV['RAILS_ENV'] || "development").dup,
18 :config => RAILS_ROOT + "/config.ru",
19 :detach => false,
20 :debugger => false,
21 :path => nil
22 }
23
24 ARGV.clone.options do |opts|
25 opts.on("-p", "--port=port", Integer,
26 "Runs Rails on the specified port.", "Default: 3000") { |v| options[:Port] = v }
27 opts.on("-b", "--binding=ip", String,
28 "Binds Rails to the specified ip.", "Default: 0.0.0.0") { |v| options[:Host] = v }
29 opts.on("-c", "--config=file", String,
30 "Use custom rackup configuration file") { |v| options[:config] = v }
31 opts.on("-d", "--daemon", "Make server run as a Daemon.") { options[:detach] = true }
32 opts.on("-u", "--debugger", "Enable ruby-debugging for the server.") { options[:debugger] = true }
33 opts.on("-e", "--environment=name", String,
34 "Specifies the environment to run this server under (test/development/production).",
35 "Default: development") { |v| options[:environment] = v }
36 opts.on("-P", "--path=/path", String, "Runs Rails app mounted at a specific path.", "Default: /") { |v| options[:path] = v }
37
38 opts.separator ""
39
40 opts.on("-h", "--help", "Show this help message.") { puts opts; exit }
41
42 opts.parse!
43 end
44
45 server = Rack::Handler.get(ARGV.first) rescue nil
46 unless server
47 begin
48 server = Rack::Handler::Mongrel
49 rescue LoadError => e
50 server = Rack::Handler::WEBrick
51 end
52 end
53
54 puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
55 puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}#{options[:path]}"
56
57 %w(cache pids sessions sockets).each do |dir_to_make|
58 FileUtils.mkdir_p(File.join(RAILS_ROOT, 'tmp', dir_to_make))
59 end
60
61 if options[:detach]
62 Process.daemon
63 pid = "#{RAILS_ROOT}/tmp/pids/server.pid"
64 File.open(pid, 'w'){ |f| f.write(Process.pid) }
65 at_exit { File.delete(pid) if File.exist?(pid) }
66 end
67
68 ENV["RAILS_ENV"] = options[:environment]
69 RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV)
70
71 if File.exist?(options[:config])
72 config = options[:config]
73 if config =~ /\.ru$/
74 cfgfile = File.read(config)
75 if cfgfile[/^#\\(.*)/]
76 opts.parse!($1.split(/\s+/))
77 end
78 inner_app = eval("Rack::Builder.new {( " + cfgfile + "\n )}.to_app", nil, config)
79 else
80 require config
81 inner_app = Object.const_get(File.basename(config, '.rb').capitalize)
82 end
83 else
84 require RAILS_ROOT + "/config/environment"
85 inner_app = ActionController::Dispatcher.new
86 end
87
88 if options[:path].nil?
89 map_path = "/"
90 else
91 ActionController::Base.relative_url_root = options[:path]
92 map_path = options[:path]
93 end
94
95 app = Rack::Builder.new {
96 use Rails::Rack::LogTailer unless options[:detach]
97 use Rails::Rack::Debugger if options[:debugger]
98 map map_path do
99 use Rails::Rack::Static
100 run inner_app
101 end
102 }.to_app
103
104 puts "=> Call with -d to detach"
105
106 trap(:INT) { exit }
107
108 puts "=> Ctrl-C to shutdown server"
109
110 begin
111 server.run(app, options.merge(:AccessLog => []))
112 ensure
113 puts 'Exiting'
114 end