2006-03-25 22:29:09 -05:00
|
|
|
require 'fileutils'
|
2008-11-22 15:48:32 -05:00
|
|
|
require 'optparse'
|
2009-11-23 20:06:10 -05:00
|
|
|
require 'action_dispatch'
|
2005-11-07 05:25:36 -05:00
|
|
|
|
2009-11-23 20:06:10 -05:00
|
|
|
module Rails
|
|
|
|
class Server < ::Rack::Server
|
|
|
|
class Options
|
|
|
|
def parse!(args)
|
2010-02-09 16:21:11 -05:00
|
|
|
args, options = args.dup, {}
|
|
|
|
|
2009-11-23 20:06:10 -05:00
|
|
|
opt_parser = OptionParser.new do |opts|
|
2010-06-22 12:42:35 -04:00
|
|
|
opts.banner = "Usage: rails server [mongrel, thin, etc] [options]"
|
2009-11-23 20:06:10 -05:00
|
|
|
opts.on("-p", "--port=port", Integer,
|
2010-02-09 09:03:18 -05:00
|
|
|
"Runs Rails on the specified port.", "Default: 3000") { |v| options[:Port] = v }
|
2009-11-23 20:06:10 -05:00
|
|
|
opts.on("-b", "--binding=ip", String,
|
2010-02-09 09:03:18 -05:00
|
|
|
"Binds Rails to the specified ip.", "Default: 0.0.0.0") { |v| options[:Host] = v }
|
2009-11-23 20:06:10 -05:00
|
|
|
opts.on("-c", "--config=file", String,
|
|
|
|
"Use custom rackup configuration file") { |v| options[:config] = v }
|
|
|
|
opts.on("-d", "--daemon", "Make server run as a Daemon.") { options[:daemonize] = true }
|
|
|
|
opts.on("-u", "--debugger", "Enable ruby-debugging for the server.") { options[:debugger] = true }
|
|
|
|
opts.on("-e", "--environment=name", String,
|
|
|
|
"Specifies the environment to run this server under (test/development/production).",
|
2010-02-09 09:03:18 -05:00
|
|
|
"Default: development") { |v| options[:environment] = v }
|
2010-07-01 20:20:26 -04:00
|
|
|
opts.on("-P","--pid=pid",String,
|
|
|
|
"Specifies the PID file.",
|
|
|
|
"Default: tmp/pids/server.pid") { |v| options[:pid] = v }
|
2008-11-22 15:48:32 -05:00
|
|
|
|
2009-11-23 20:06:10 -05:00
|
|
|
opts.separator ""
|
2008-11-22 15:48:32 -05:00
|
|
|
|
2009-11-23 20:06:10 -05:00
|
|
|
opts.on("-h", "--help", "Show this help message.") { puts opts; exit }
|
|
|
|
end
|
2008-11-22 15:48:32 -05:00
|
|
|
|
2009-11-23 20:06:10 -05:00
|
|
|
opt_parser.parse! args
|
2008-11-22 15:48:32 -05:00
|
|
|
|
2009-11-23 20:06:10 -05:00
|
|
|
options[:server] = args.shift
|
|
|
|
options
|
|
|
|
end
|
|
|
|
end
|
2006-06-28 19:41:29 -04:00
|
|
|
|
2010-02-09 16:21:11 -05:00
|
|
|
def initialize(*)
|
|
|
|
super
|
|
|
|
set_environment
|
|
|
|
end
|
|
|
|
|
2011-02-28 18:47:03 -05:00
|
|
|
def app
|
2011-06-06 16:56:41 -04:00
|
|
|
@app ||= super.respond_to?(:to_app) ? super.to_app : super
|
2011-02-28 18:47:03 -05:00
|
|
|
end
|
|
|
|
|
2009-11-23 20:06:10 -05:00
|
|
|
def opt_parser
|
|
|
|
Options.new
|
|
|
|
end
|
2008-06-25 17:40:00 -04:00
|
|
|
|
2010-02-09 16:21:11 -05:00
|
|
|
def set_environment
|
|
|
|
ENV["RAILS_ENV"] ||= options[:environment]
|
|
|
|
end
|
2010-01-11 17:01:28 -05:00
|
|
|
|
2010-02-09 16:21:11 -05:00
|
|
|
def start
|
2011-05-06 02:22:27 -04:00
|
|
|
url = "#{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}"
|
2009-11-23 20:06:10 -05:00
|
|
|
puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
|
2011-05-06 02:22:27 -04:00
|
|
|
puts "=> Rails #{Rails.version} application starting in #{Rails.env} on #{url}"
|
2009-11-23 20:06:10 -05:00
|
|
|
puts "=> Call with -d to detach" unless options[:daemonize]
|
|
|
|
trap(:INT) { exit }
|
|
|
|
puts "=> Ctrl-C to shutdown server" unless options[:daemonize]
|
2008-11-22 15:48:32 -05:00
|
|
|
|
2010-03-31 05:58:48 -04:00
|
|
|
#Create required tmp directories if not found
|
|
|
|
%w(cache pids sessions sockets).each do |dir_to_make|
|
|
|
|
FileUtils.mkdir_p(Rails.root.join('tmp', dir_to_make))
|
|
|
|
end
|
|
|
|
|
2009-11-23 20:06:10 -05:00
|
|
|
super
|
|
|
|
ensure
|
2010-02-06 06:48:02 -05:00
|
|
|
# The '-h' option calls exit before @options is set.
|
|
|
|
# If we call 'options' with it unset, we get double help banners.
|
|
|
|
puts 'Exiting' unless @options && options[:daemonize]
|
2009-11-23 20:06:10 -05:00
|
|
|
end
|
2008-11-22 15:48:32 -05:00
|
|
|
|
2009-11-23 20:06:10 -05:00
|
|
|
def middleware
|
|
|
|
middlewares = []
|
2010-01-21 07:05:30 -05:00
|
|
|
middlewares << [Rails::Rack::LogTailer, log_path] unless options[:daemonize]
|
2009-11-23 20:06:10 -05:00
|
|
|
middlewares << [Rails::Rack::Debugger] if options[:debugger]
|
2011-05-23 04:27:46 -04:00
|
|
|
middlewares << [Rails::Rack::ContentLength]
|
2009-11-23 20:06:10 -05:00
|
|
|
Hash.new(middlewares)
|
|
|
|
end
|
2008-11-22 15:48:32 -05:00
|
|
|
|
2010-01-15 06:15:49 -05:00
|
|
|
def log_path
|
|
|
|
"log/#{options[:environment]}.log"
|
|
|
|
end
|
|
|
|
|
2009-11-23 20:06:10 -05:00
|
|
|
def default_options
|
2009-12-26 19:21:36 -05:00
|
|
|
super.merge({
|
2009-11-23 20:06:10 -05:00
|
|
|
:Port => 3000,
|
|
|
|
:environment => (ENV['RAILS_ENV'] || "development").dup,
|
|
|
|
:daemonize => false,
|
|
|
|
:debugger => false,
|
2010-05-05 14:42:10 -04:00
|
|
|
:pid => File.expand_path("tmp/pids/server.pid"),
|
|
|
|
:config => File.expand_path("config.ru")
|
2009-12-26 19:21:36 -05:00
|
|
|
})
|
2009-11-23 20:06:10 -05:00
|
|
|
end
|
|
|
|
end
|
2008-11-22 15:48:32 -05:00
|
|
|
end
|