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

Comments

rtomayko Wed Nov 26 10:13:03 -0800 2008

Hell yes.

boblmartens Wed Nov 26 11:01:38 -0800 2008

Thank you for this.

sporkmonger Wed Nov 26 12:09:15 -0800 2008

Woohoo! About time!