diff options
-rw-r--r-- | railties/lib/commands/servers/lighttpd.rb | 45 | ||||
-rw-r--r-- | railties/lib/commands/servers/webrick.rb | 59 |
2 files changed, 104 insertions, 0 deletions
diff --git a/railties/lib/commands/servers/lighttpd.rb b/railties/lib/commands/servers/lighttpd.rb new file mode 100644 index 0000000000..df6ad663f9 --- /dev/null +++ b/railties/lib/commands/servers/lighttpd.rb @@ -0,0 +1,45 @@ +require 'active_support' + +unless RUBY_PLATFORM !~ /mswin/ && !silence_stderr { `lighttpd -version` }.blank? + puts "lighttpd is not available on your system (or not in your path)" + exit 1 +end + +def tail_f(input) + loop do + line = input.gets + yield line if line + if input.eof? + sleep 1 + input.seek(input.tell) + end + end +end + +config_file = "#{RAILS_ROOT}/config/lighttpd.conf" + +port = IO.read(config_file).scan(/^server.port\s*=\s*(\d+)/).first rescue 3000 +puts "=> Rails application started on http://0.0.0.0:#{port}" + +if ARGV.first == "-d" + puts "=> Configure in config/lighttpd.conf" + detach = true +else + puts "=> Call with -d to detach (requires absolute paths in config/lighttpd.conf)" + puts "=> Ctrl-C to shutdown server (see config/lighttpd.conf for options)" + detach = false + + Process.detach(fork do + begin + File.open("#{RAILS_ROOT}/log/#{RAILS_ENV}.log", 'r') do |log| + log.seek(0, IO::SEEK_END) + tail_f(log) {|line| puts line} + end + rescue Exception + end + exit + end) +end + +trap(:INT) {exit} +`lighttpd #{!detach ? "-D " : ""}-f #{config_file}` diff --git a/railties/lib/commands/servers/webrick.rb b/railties/lib/commands/servers/webrick.rb new file mode 100644 index 0000000000..db8e8b3d93 --- /dev/null +++ b/railties/lib/commands/servers/webrick.rb @@ -0,0 +1,59 @@ +require 'webrick' +require 'optparse' + +OPTIONS = { + :port => 3000, + :ip => "0.0.0.0", + :environment => (ENV['RAILS_ENV'] || "development").dup, + :server_root => File.expand_path(RAILS_ROOT + "/public/"), + :server_type => WEBrick::SimpleServer, + :charset => "UTF-8", + :mime_types => WEBrick::HTTPUtils::DefaultMimeTypes +} + +ARGV.options do |opts| + script_name = File.basename($0) + opts.banner = "Usage: ruby #{script_name} [options]" + + opts.separator "" + + opts.on("-p", "--port=port", Integer, + "Runs Rails on the specified port.", + "Default: 3000") { |OPTIONS[:port]| } + opts.on("-b", "--binding=ip", String, + "Binds Rails to the specified ip.", + "Default: 0.0.0.0") { |OPTIONS[:ip]| } + opts.on("-e", "--environment=name", String, + "Specifies the environment to run this server under (test/development/production).", + "Default: development") { |OPTIONS[:environment]| } + opts.on("-m", "--mime-types=filename", String, + "Specifies an Apache style mime.types configuration file to be used for mime types", + "Default: none") { |mime_types_file| OPTIONS[:mime_types] = WEBrick::HTTPUtils::load_mime_types(mime_types_file) } + + opts.on("-d", "--daemon", + "Make Rails run as a Daemon (only works if fork is available -- meaning on *nix)." + ) { OPTIONS[:server_type] = WEBrick::Daemon } + + opts.on("-c", "--charset=charset", String, + "Set default charset for output.", + "Default: UTF-8") { |OPTIONS[:charset]| } + + opts.separator "" + + opts.on("-h", "--help", + "Show this help message.") { puts opts; exit } + + opts.parse! +end + +ENV["RAILS_ENV"] = OPTIONS[:environment] +RAILS_ENV.replace(OPTIONS[:environment]) if defined?(RAILS_ENV) + +require RAILS_ROOT + "/config/environment" +require 'webrick_server' + +OPTIONS['working_directory'] = File.expand_path(RAILS_ROOT) + +puts "=> Rails application started on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}" +puts "=> Ctrl-C to shutdown server; call with --help for options" if OPTIONS[:server_type] == WEBrick::SimpleServer +DispatchServlet.dispatch(OPTIONS) |