Permalink
Browse files

Refactor rackup into Rack::Server

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
1 parent 87957c6 commit 5db5d4e7432d026a7bddf344f97e8facd09128d8 Yehuda Katz + Carl Lerche committed with josh Nov 21, 2009
Showing with 267 additions and 183 deletions.
  1. +2 −176 bin/rackup
  2. +1 −0 lib/rack.rb
  3. +15 −0 lib/rack/builder.rb
  4. +19 −0 lib/rack/handler.rb
  5. +189 −0 lib/rack/server.rb
  6. +8 −2 test/rackup/config.ru
  7. +33 −5 test/spec_rackup.rb
View
@@ -1,176 +1,2 @@
-#!/usr/bin/env ruby
-# -*- ruby -*-
-
-require 'rack'
-
-require 'optparse'
-
-automatic = false
-server = nil
-env = "development"
-daemonize = false
-pid = nil
-options = {:Port => 9292, :Host => "0.0.0.0", :AccessLog => []}
-
-# Don't evaluate CGI ISINDEX parameters.
-# http://hoohoo.ncsa.uiuc.edu/cgi/cl.html
-ARGV.clear if ENV.include?("REQUEST_METHOD")
-
-opts = OptionParser.new("", 24, ' ') { |opts|
- opts.banner = "Usage: rackup [ruby options] [rack options] [rackup config]"
-
- opts.separator ""
- opts.separator "Ruby options:"
-
- lineno = 1
- opts.on("-e", "--eval LINE", "evaluate a LINE of code") { |line|
- eval line, TOPLEVEL_BINDING, "-e", lineno
- lineno += 1
- }
-
- opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") {
- $DEBUG = true
- }
- opts.on("-w", "--warn", "turn warnings on for your script") {
- $-w = true
- }
-
- opts.on("-I", "--include PATH",
- "specify $LOAD_PATH (may be used more than once)") { |path|
- $LOAD_PATH.unshift(*path.split(":"))
- }
-
- opts.on("-r", "--require LIBRARY",
- "require the library, before executing your script") { |library|
- require library
- }
-
- opts.separator ""
- opts.separator "Rack options:"
- opts.on("-s", "--server SERVER", "serve using SERVER (webrick/mongrel)") { |s|
- server = s
- }
-
- opts.on("-o", "--host HOST", "listen on HOST (default: 0.0.0.0)") { |host|
- options[:Host] = host
- }
-
- opts.on("-p", "--port PORT", "use PORT (default: 9292)") { |port|
- options[:Port] = port
- }
-
- opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e|
- env = e
- }
-
- opts.on("-D", "--daemonize", "run daemonized in the background") { |d|
- daemonize = d ? true : false
- }
-
- opts.on("-P", "--pid FILE", "file to store PID (default: rack.pid)") { |f|
- pid = File.expand_path(f)
- }
-
- opts.separator ""
- opts.separator "Common options:"
-
- opts.on_tail("-h", "--help", "Show this message") do
- puts opts
- exit
- end
-
- opts.on_tail("--version", "Show version") do
- puts "Rack #{Rack.version}"
- exit
- end
-
- opts.parse! ARGV
-}
-
-require 'pp' if $DEBUG
-
-config = ARGV[0] || "config.ru"
-if !File.exist? config
- abort "configuration #{config} not found"
-end
-
-if config =~ /\.ru$/
- cfgfile = File.read(config)
- if cfgfile[/^#\\(.*)/]
- opts.parse! $1.split(/\s+/)
- end
- cfgfile.sub!(/^__END__\n.*/, '')
- inner_app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app",
- nil, config
-else
- require config
- inner_app = Object.const_get(File.basename(config, '.rb').capitalize)
-end
-
-unless server = Rack::Handler.get(server)
- # Guess.
- if ENV.include?("PHP_FCGI_CHILDREN")
- server = Rack::Handler::FastCGI
-
- # We already speak FastCGI
- options.delete :File
- options.delete :Port
- elsif ENV.include?("REQUEST_METHOD")
- server = Rack::Handler::CGI
- else
- begin
- server = Rack::Handler::Mongrel
- rescue LoadError => e
- server = Rack::Handler::WEBrick
- end
- end
-end
-
-p server if $DEBUG
-
-case env
-when "development"
- app = Rack::Builder.new {
- use Rack::CommonLogger, $stderr unless server.name =~ /CGI/
- use Rack::ShowExceptions
- use Rack::Lint
- run inner_app
- }.to_app
-
-when "deployment"
- app = Rack::Builder.new {
- use Rack::CommonLogger, $stderr unless server.name =~ /CGI/
- run inner_app
- }.to_app
-
-when "none"
- app = inner_app
-
-end
-
-if $DEBUG
- pp app
- pp inner_app
-end
-
-if daemonize
- if RUBY_VERSION < "1.9"
- exit if fork
- Process.setsid
- exit if fork
- Dir.chdir "/"
- File.umask 0000
- STDIN.reopen "/dev/null"
- STDOUT.reopen "/dev/null", "a"
- STDERR.reopen "/dev/null", "a"
- else
- Process.daemon
- end
-
- if pid
- File.open(pid, 'w'){ |f| f.write("#{Process.pid}") }
- at_exit { File.delete(pid) if File.exist?(pid) }
- end
-end
-
-server.run app, options
+require "rack"
+Rack::Server.start
View
@@ -42,6 +42,7 @@ def self.release
autoload :Mime, "rack/mime"
autoload :Recursive, "rack/recursive"
autoload :Reloader, "rack/reloader"
+ autoload :Server, "rack/server"
autoload :ShowExceptions, "rack/showexceptions"
autoload :ShowStatus, "rack/showstatus"
autoload :Static, "rack/static"
View
@@ -24,6 +24,21 @@ module Rack
# You can use +map+ to construct a Rack::URLMap in a convenient way.
class Builder
+ def self.parse_file(config, opts = nil)
+ if config =~ /\.ru$/
+ cfgfile = ::File.read(config)
+ if cfgfile[/^#\\(.*)/] && opts
+ opts.parse! $1.split(/\s+/)
+ end
+ cfgfile.sub!(/^__END__\n.*/, '')
+ eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app",
+ TOPLEVEL_BINDING, config
+ else
+ require config
+ Object.const_get(::File.basename(config, '.rb').capitalize)
+ end
+ end
+
def initialize(&block)
@ins = []
instance_eval(&block) if block_given?
View
@@ -22,6 +22,25 @@ def self.get(server)
end
end
+ def self.default(options = {})
+ # Guess.
+ if ENV.include?("PHP_FCGI_CHILDREN")
+ # We already speak FastCGI
+ options.delete :File
+ options.delete :Port
+
+ Rack::Handler::FastCGI
+ elsif ENV.include?("REQUEST_METHOD")
+ Rack::Handler::CGI
+ else
+ begin
+ Rack::Handler::Mongrel
+ rescue LoadError => e
+ Rack::Handler::WEBrick
+ end
+ end
+ end
+
# Transforms server-name constants to their canonical form as filenames,
# then tries to require them but silences the LoadError if not found
#
Oops, something went wrong.

0 comments on commit 5db5d4e

Please sign in to comment.