Object
# File lib/rack/server.rb, line 206 206: def self.logging_middleware 207: lambda { |server| 208: server.server.name =~ /CGI/ ? nil : [Rack::CommonLogger, $stderr] 209: } 210: end
# File lib/rack/server.rb, line 212 212: def self.middleware 213: @middleware ||= begin 214: m = Hash.new {|h,k| h[k] = []} 215: m["deployment"].concat [ 216: [Rack::ContentLength], 217: [Rack::Chunked], 218: logging_middleware 219: ] 220: m["development"].concat m["deployment"] + [[Rack::ShowExceptions], [Rack::Lint]] 221: m 222: end 223: 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 174 174: def initialize(options = nil) 175: @options = options 176: @app = options[:app] if options && options[:app] 177: 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 136 136: def self.start(options = nil) 137: new(options).start 138: end
# File lib/rack/server.rb, line 194 194: def app 195: @app ||= begin 196: if !::File.exist? options[:config] 197: abort "configuration #{options[:config]} not found" 198: end 199: 200: app, options = Rack::Builder.parse_file(self.options[:config], opt_parser) 201: self.options.merge! options 202: app 203: end 204: end
# File lib/rack/server.rb, line 183 183: def default_options 184: { 185: :environment => ENV['RACK_ENV'] || "development", 186: :pid => nil, 187: :Port => 9292, 188: :Host => "0.0.0.0", 189: :AccessLog => [], 190: :config => "config.ru" 191: } 192: end
# File lib/rack/server.rb, line 225 225: def middleware 226: self.class.middleware 227: end
# File lib/rack/server.rb, line 179 179: def options 180: @options ||= parse_options(ARGV) 181: end
# File lib/rack/server.rb, line 268 268: def server 269: @_server ||= Rack::Handler.get(options[:server]) || Rack::Handler.default(options) 270: end
# File lib/rack/server.rb, line 229 229: def start &blk 230: if options[:warn] 231: $-w = true 232: end 233: 234: if includes = options[:include] 235: $LOAD_PATH.unshift(*includes) 236: end 237: 238: if library = options[:require] 239: require library 240: end 241: 242: if options[:debug] 243: $DEBUG = true 244: require 'pp' 245: p options[:server] 246: pp wrapped_app 247: pp app 248: end 249: 250: # Touch the wrapped app, so that the config.ru is loaded before 251: # daemonization (i.e. before chdir, etc). 252: wrapped_app 253: 254: daemonize_app if options[:daemonize] 255: write_pid if options[:pid] 256: 257: trap(:INT) do 258: if server.respond_to?(:shutdown) 259: server.shutdown 260: else 261: exit 262: end 263: end 264: 265: server.run wrapped_app, options, &blk 266: end
# File lib/rack/server.rb, line 290 290: def build_app(app) 291: middleware[options[:environment]].reverse_each do |middleware| 292: middleware = middleware.call(self) if middleware.respond_to?(:call) 293: next unless middleware 294: klass = middleware.shift 295: app = klass.new(app, *middleware) 296: end 297: app 298: end
# File lib/rack/server.rb, line 304 304: def daemonize_app 305: if RUBY_VERSION < "1.9" 306: exit if fork 307: Process.setsid 308: exit if fork 309: Dir.chdir "/" 310: STDIN.reopen "/dev/null" 311: STDOUT.reopen "/dev/null", "a" 312: STDERR.reopen "/dev/null", "a" 313: else 314: Process.daemon 315: end 316: end
# File lib/rack/server.rb, line 286 286: def opt_parser 287: Options.new 288: end
# File lib/rack/server.rb, line 273 273: def parse_options(args) 274: options = default_options 275: 276: # Don't evaluate CGI ISINDEX parameters. 277: # http://hoohoo.ncsa.uiuc.edu/cgi/cl.html 278: args.clear if ENV.include?("REQUEST_METHOD") 279: 280: options.merge! opt_parser.parse!(args) 281: options[:config] = ::File.expand_path(options[:config]) 282: ENV["RACK_ENV"] = options[:environment] 283: options 284: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.