|
1 | 1 | #!/usr/bin/env ruby
|
| 2 | +require File.dirname(__FILE__) + '/../config/boot' |
| 3 | +# Don't do this. Instead, put it here, where we can customize it. |
| 4 | +#require 'commands/server' |
2 | 5 |
|
3 |
| -require 'webrick' |
4 |
| -require 'optparse' |
| 6 | +require 'active_support' |
| 7 | +require 'action_controller' |
5 | 8 |
|
6 |
| -OPTIONS = { |
7 |
| - :port => 2500, |
8 |
| - :ip => "0.0.0.0", |
9 |
| - :environment => "production", |
10 |
| - :server_root => File.expand_path(File.dirname(__FILE__) + "/../public/"), |
11 |
| - :server_type => WEBrick::SimpleServer, |
12 |
| - :mime_types => WEBrick::HTTPUtils::DefaultMimeTypes.merge({ |
13 |
| - 'avi' => 'video/x-msvideo', |
14 |
| - 'gz' => 'application/x-gzip', |
15 |
| - 'js' => 'application/x-javascript', |
16 |
| - 'nb' => 'application/mathematica', |
17 |
| - 'pdf' => 'application/pdf', |
18 |
| - 'svg' => 'application/svg+xml', |
19 |
| - 'tar' => 'application/x-tar', |
20 |
| - 'tex' => 'application/x-tex', |
21 |
| - 'xhtml' => 'application/xhtml+xml', |
22 |
| - 'xml' => 'application/xml', |
23 |
| - 'xslt' => 'application/xslt+xml' |
24 |
| - }) |
25 |
| -} |
| 9 | +require 'fileutils' |
| 10 | +require 'optparse' |
26 | 11 |
|
27 |
| -ARGV.options do |opts| |
28 |
| - script_name = File.basename($0) |
29 |
| - opts.banner = "Usage: ruby #{script_name} [options]" |
| 12 | +# TODO: Push Thin adapter upstream so we don't need worry about requiring it |
| 13 | +begin |
| 14 | + require_library_or_gem 'thin' |
| 15 | +rescue Exception |
| 16 | + # Thin not available |
| 17 | +end |
30 | 18 |
|
31 |
| - opts.separator "" |
| 19 | +options = { |
| 20 | + :Port => 2500, |
| 21 | + :Host => "0.0.0.0", |
| 22 | + :environment => (ENV['RAILS_ENV'] || "production").dup, |
| 23 | + :config => RAILS_ROOT + "/config.ru", |
| 24 | + :detach => false, |
| 25 | + :debugger => false |
| 26 | +} |
32 | 27 |
|
| 28 | +ARGV.clone.options do |opts| |
33 | 29 | opts.on("-p", "--port=port", Integer,
|
34 |
| - "Runs Instiki on the specified port.", |
35 |
| - "Default: 2500") { } |
36 |
| -# "Default: 2500") { |OPTIONS[:port]| } |
| 30 | + "Runs Instiki on the specified port.", "Default: 2500") { |v| options[:Port] = v } |
37 | 31 | opts.on("-b", "--binding=ip", String,
|
38 |
| - "Binds Instiki to the specified ip.", |
39 |
| - "Default: 0.0.0.0") { } |
40 |
| -# "Default: 0.0.0.0") { |OPTIONS[:ip]| } |
| 32 | + "Binds Instiki to the specified ip.", "Default: 0.0.0.0") { |v| options[:Host] = v } |
| 33 | + opts.on("-c", "--config=file", String, |
| 34 | + "Use custom rackup configuration file") { |v| options[:config] = v } |
| 35 | + opts.on("-d", "--daemon", "Make server run as a Daemon.") { options[:detach] = true } |
| 36 | + opts.on("-u", "--debugger", "Enable ruby-debugging for the server.") { options[:debugger] = true } |
41 | 37 | opts.on("-e", "--environment=name", String,
|
42 | 38 | "Specifies the environment to run this server under (test/development/production).",
|
43 |
| - "Default: development") { } |
44 |
| -# "Default: development") { |OPTIONS[:environment]| } |
45 |
| - opts.on("-d", "--daemon", |
46 |
| - "Make Instiki run as a Daemon (only works if fork is available -- meaning on *nix)." |
47 |
| - ) { OPTIONS[:server_type] = WEBrick::Daemon } |
| 39 | + "Default: production") { |v| options[:environment] = v } |
48 | 40 |
|
49 | 41 | opts.separator ""
|
50 | 42 |
|
51 |
| - opts.on("-h", "--help", |
52 |
| - "Show this help message.") { puts opts; exit } |
| 43 | + opts.on("-h", "--help", "Show this help message.") { puts opts; exit } |
53 | 44 |
|
54 | 45 | opts.parse!
|
55 | 46 | end
|
56 | 47 |
|
57 |
| -ENV["RAILS_ENV"] = OPTIONS[:environment] |
58 |
| -require File.dirname(__FILE__) + "/../config/environment" |
59 |
| -require 'webrick_server' |
| 48 | +server = Rack::Handler.get(ARGV.first) rescue nil |
| 49 | +unless server |
| 50 | + begin |
| 51 | + server = Rack::Handler::Mongrel |
| 52 | + rescue LoadError => e |
| 53 | + server = Rack::Handler::WEBrick |
| 54 | + end |
| 55 | +end |
60 | 56 |
|
61 |
| -OPTIONS['working_directory'] = File.expand_path(RAILS_ROOT) |
| 57 | +puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}" |
| 58 | +puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}" |
62 | 59 |
|
63 |
| -puts "=> Instiki started on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}" |
64 |
| -puts "=> Ctrl-C to shutdown; call with --help for options" if OPTIONS[:server_type] == WEBrick::SimpleServer |
65 |
| -DispatchServlet.dispatch(OPTIONS) |
| 60 | +%w(cache pids sessions sockets).each do |dir_to_make| |
| 61 | + FileUtils.mkdir_p(File.join(RAILS_ROOT, 'tmp', dir_to_make)) |
| 62 | +end |
| 63 | + |
| 64 | +if options[:detach] |
| 65 | + Process.daemon |
| 66 | + pid = "#{RAILS_ROOT}/tmp/pids/server.pid" |
| 67 | + File.open(pid, 'w'){ |f| f.write(Process.pid) } |
| 68 | + at_exit { File.delete(pid) if File.exist?(pid) } |
| 69 | +end |
| 70 | + |
| 71 | +ENV["RAILS_ENV"] = options[:environment] |
| 72 | +RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV) |
| 73 | + |
| 74 | +if File.exist?(options[:config]) |
| 75 | + config = options[:config] |
| 76 | + if config =~ /\.ru$/ |
| 77 | + cfgfile = File.read(config) |
| 78 | + if cfgfile[/^#\\(.*)/] |
| 79 | + opts.parse!($1.split(/\s+/)) |
| 80 | + end |
| 81 | + inner_app = eval("Rack::Builder.new {( " + cfgfile + "\n )}.to_app", nil, config) |
| 82 | + else |
| 83 | + require config |
| 84 | + inner_app = Object.const_get(File.basename(config, '.rb').capitalize) |
| 85 | + end |
| 86 | +else |
| 87 | + require RAILS_ROOT + "/config/environment" |
| 88 | + inner_app = ActionController::Dispatcher.new |
| 89 | +end |
| 90 | + |
| 91 | +app = Rack::Builder.new { |
| 92 | + use Rails::Rack::LogTailer unless options[:detach] |
| 93 | + use Rails::Rack::Static |
| 94 | + use Rails::Rack::Debugger if options[:debugger] |
| 95 | + run inner_app |
| 96 | +}.to_app |
| 97 | + |
| 98 | +puts "=> Call with -d to detach" |
| 99 | + |
| 100 | +trap(:INT) { exit } |
| 101 | + |
| 102 | +puts "=> Ctrl-C to shutdown server" |
| 103 | + |
| 104 | +begin |
| 105 | + server.run(app, options.merge(:AccessLog => [])) |
| 106 | +ensure |
| 107 | + puts 'Exiting' |
| 108 | +end |
0 commit comments