Skip to content
Browse files

Add rackup, an experimental standalone Rack app starter

darcs-hash:20070224180326-4fc50-6c3c26b7baa8db05b7e48c37b5c90c2b4e4108e8.gz
  • Loading branch information...
1 parent 6561fb3 commit f65969ccac8f6098acee9b1ab506283dfcdf18a5 @chneukirchen chneukirchen committed Feb 24, 2007
Showing with 132 additions and 0 deletions.
  1. +132 −0 bin/rackup
View
132 bin/rackup
@@ -0,0 +1,132 @@
+#!/usr/bin/env ruby
+# -*- ruby -*-
+
+require 'optparse'
+
+automatic = false
+server = nil
+env = "development"
+options = {:Port => 9292, :Host => "0.0.0.0", :AccessLog => []}
+
+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.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
+ require 'rack'
+ puts "Rack #{Rack.version}"
+ exit
+ end
+
+ opts.parse! ARGV
+}
+
+config = ARGV[0] || "config.ru"
+if !File.exist? config
+ abort "configuration #{config} not found"
+end
+
+require 'rack'
+
+case server
+when nil
+ begin
+ server = Rack::Handler::Mongrel
+ rescue LoadError => e
+ server = Rack::Handler::WEBrick
+ end
+when "mongrel"
+ server = Rack::Handler::Mongrel
+when "webrick"
+ server = Rack::Handler::WEBrick
+else
+ server = Rack::Handler.const_get(server.capitalize)
+end
+
+p server if $DEBUG
+
+if config =~ /\.ru$/
+ inner_app = eval "Rack::Builder.new {( " + File.read(config) + "\n )}.to_app",
+ nil, config
+else
+ require config
+ inner_app = Object.const_get(File.basename(config, '.rb').capitalize)
+end
+
+case env
+when "development"
+ app = Rack::Builder.new {
+ use Rack::CommonLogger, STDERR
+ use Rack::ShowExceptions
+ use Rack::Lint
+ run inner_app
+ }.to_app
+
+when "deployment"
+ app = Rack::Builder.new {
+ use Rack::CommonLogger, STDERR
+ run inner_app
+ }.to_app
+
+when "none"
+ app = inner_app
+
+end
+
+if $DEBUG
+ pp app
+ pp inner_app
+end
+
+server.run app, options

0 comments on commit f65969c

Please sign in to comment.
Something went wrong with that request. Please try again.