server 2.1 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1 2 3 4 5 6
#!/usr/local/bin/ruby

require 'webrick'
require 'optparse'

OPTIONS = {
7 8 9 10 11
  :port            => 3000,
  :ip              => "0.0.0.0",
  :environment     => "development",
  :server_root     => File.expand_path(File.dirname(__FILE__) + "/../public/"),
  :server_type     => WEBrick::SimpleServer,
12 13
  :charset         => "UTF-8",
  :mime_types      => WEBrick::HTTPUtils::DefaultMimeTypes
D
Initial  
David Heinemeier Hansson 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26
}

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.",
27
          "Default: 0.0.0.0") { |OPTIONS[:ip]| }
D
Initial  
David Heinemeier Hansson 已提交
28 29 30
  opts.on("-e", "--environment=name", String,
          "Specifies the environment to run this server under (test/development/production).",
          "Default: development") { |OPTIONS[:environment]| }
31 32 33 34
  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) }

D
Initial  
David Heinemeier Hansson 已提交
35 36 37 38
  opts.on("-d", "--daemon",
          "Make Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
          ) { OPTIONS[:server_type] = WEBrick::Daemon }

39 40 41 42
  opts.on("-c", "--charset=charset", String,
          "Set default charset for output.",
          "Default: UTF-8") { |OPTIONS[:charset]| }

D
Initial  
David Heinemeier Hansson 已提交
43 44 45 46 47 48 49 50 51 52 53 54
  opts.separator ""

  opts.on("-h", "--help",
          "Show this help message.") { puts opts; exit }

  opts.parse!
end

ENV["RAILS_ENV"] = OPTIONS[:environment]
require File.dirname(__FILE__) + "/../config/environment"
require 'webrick_server'

55 56
OPTIONS['working_directory'] = File.expand_path(RAILS_ROOT)

D
Initial  
David Heinemeier Hansson 已提交
57
puts "=> Rails application started on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}"
58
puts "=> Ctrl-C to shutdown server; call with --help for options" if OPTIONS[:server_type] == WEBrick::SimpleServer
59
DispatchServlet.dispatch(OPTIONS)