Object
# File lib/rack/server.rb, line 168 168: def self.middleware 169: @middleware ||= begin 170: m = Hash.new {|h,k| h[k] = []} 171: m["deployment"].concat [lambda {|server| server.server.name =~ /CGI/ ? nil : [Rack::CommonLogger, $stderr] }] 172: m["development"].concat m["deployment"] + [[Rack::ShowExceptions], [Rack::Lint]] 173: m 174: end 175: end
Options may include:
:app
a rack application to run (overrides :config)
:config
a rackup configuration file path to load (.ru)
:environment
this selects the middleware that will be wrapped around your application. Default options available are: * development: CommonLogger, ShowExceptions, and Lint * deployment: CommonLogger * none: no extra middleware note: when the server is a cgi server, CommonLogger is not included.
:server
choose a specific Rack::Handler, e.g. cgi, fcgi, webrick
:daemonize
if true, the server will daemonize itself (fork, detach, etc)
:pid
path to write a pid file after daemonize
:Host
the host address to bind to (used by supporting Rack::Handler)
:Port
the port to bind to (used by supporting Rack::Handler)
:AccessLog
webrick acess log options (or supporting Rack::Handler)
:debug
turn on debug output ($DEBUG = true)
:warn
turn on warnings ($-w = true)
:include
add given paths to $LOAD_PATH
:require
require the given libraries
# File lib/rack/server.rb, line 137 137: def initialize(options = nil) 138: @options = options 139: end
Start a new rack server (like running rackup). This will parse ARGV and provide standard ARGV rackup options, defaulting to load ‘config.ru’.
Providing an options hash will prevent ARGV parsing and will not include any default options.
This method can be used to very easily launch a CGI application, for example:
Rack::Server.start( :app => lambda do |e| [200, {'Content-Type' => 'text/html'}, ['hello world']] end, :server => 'cgi' )
Further options available here are documented on Rack::Server#initialize
# File lib/rack/server.rb, line 99 99: def self.start(options = nil) 100: new(options).start 101: end
# File lib/rack/server.rb, line 156 156: def app 157: @app ||= begin 158: if !::File.exist? options[:config] 159: abort "configuration #{options[:config]} not found" 160: end 161: 162: app, options = Rack::Builder.parse_file(self.options[:config], opt_parser) 163: self.options.merge! options 164: app 165: end 166: end
# File lib/rack/server.rb, line 145 145: def default_options 146: { 147: :environment => "development", 148: :pid => nil, 149: :Port => 9292, 150: :Host => "0.0.0.0", 151: :AccessLog => [], 152: :config => "config.ru" 153: } 154: end
# File lib/rack/server.rb, line 177 177: def middleware 178: self.class.middleware 179: end
# File lib/rack/server.rb, line 141 141: def options 142: @options ||= parse_options(ARGV) 143: end
# File lib/rack/server.rb, line 216 216: def server 217: @_server ||= Rack::Handler.get(options[:server]) || Rack::Handler.default(options) 218: end
# File lib/rack/server.rb, line 181 181: def start 182: if options[:debug] 183: $DEBUG = true 184: require 'pp' 185: p options[:server] 186: pp wrapped_app 187: pp app 188: end 189: 190: if options[:warn] 191: $-w = true 192: end 193: 194: if includes = options[:include] 195: $LOAD_PATH.unshift(*includes) 196: end 197: 198: if library = options[:require] 199: require library 200: end 201: 202: daemonize_app if options[:daemonize] 203: write_pid if options[:pid] 204: 205: trap(:INT) do 206: if server.respond_to?(:shutdown) 207: server.shutdown 208: else 209: exit 210: end 211: end 212: 213: server.run wrapped_app, options 214: end
# File lib/rack/server.rb, line 237 237: def build_app(app) 238: middleware[options[:environment]].reverse_each do |middleware| 239: middleware = middleware.call(self) if middleware.respond_to?(:call) 240: next unless middleware 241: klass = middleware.shift 242: app = klass.new(app, *middleware) 243: end 244: app 245: end
# File lib/rack/server.rb, line 251 251: def daemonize_app 252: if RUBY_VERSION < "1.9" 253: exit if fork 254: Process.setsid 255: exit if fork 256: Dir.chdir "/" 257: ::File.umask 0000 258: STDIN.reopen "/dev/null" 259: STDOUT.reopen "/dev/null", "a" 260: STDERR.reopen "/dev/null", "a" 261: else 262: Process.daemon 263: end 264: end
# File lib/rack/server.rb, line 233 233: def opt_parser 234: Options.new 235: end
# File lib/rack/server.rb, line 221 221: def parse_options(args) 222: options = default_options 223: 224: # Don't evaluate CGI ISINDEX parameters. 225: # http://hoohoo.ncsa.uiuc.edu/cgi/cl.html 226: args.clear if ENV.include?("REQUEST_METHOD") 227: 228: options.merge! opt_parser.parse! args 229: ENV["RACK_ENV"] = options[:environment] 230: options 231: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.