Permalink
Browse files

Merge pull request #480 from udzura/master

Add a rackup option for a one-liner rack app server
  • Loading branch information...
2 parents 1e75faa + 3edd1e8 commit 7e1f081e1ed080b7efa6cddd9bf885d4bcfc60e2 @raggi raggi committed Jan 4, 2013
Showing with 38 additions and 11 deletions.
  1. +6 −2 lib/rack/builder.rb
  2. +19 −9 lib/rack/server.rb
  3. +7 −0 test/spec_builder.rb
  4. +6 −0 test/spec_server.rb
View
8 lib/rack/builder.rb
@@ -37,15 +37,19 @@ def self.parse_file(config, opts = Server::Options.new)
options = opts.parse! $1.split(/\s+/)
end
cfgfile.sub!(/^__END__\n.*\Z/m, '')
- app = eval "Rack::Builder.new {\n" + cfgfile + "\n}.to_app",
- TOPLEVEL_BINDING, config, 0
+ app = new_from_string cfgfile, config
else
require config
app = Object.const_get(::File.basename(config, '.rb').capitalize)
end
return app, options
end
+ def self.new_from_string(builder_script, file="(rackup)")
+ eval "Rack::Builder.new {\n" + builder_script + "\n}.to_app",
+ TOPLEVEL_BINDING, file, 0
+ end
+
def initialize(default_app = nil,&block)
@use, @map, @run = [], nil, default_app
instance_eval(&block) if block_given?
View
28 lib/rack/server.rb
@@ -17,6 +17,10 @@ def parse!(args)
lineno += 1
}
+ opts.on("-b", "--builder BUILDER_LINE", "evaluate a BUILDER_LINE of code as a builder script") { |line|
+ options[:builder] = line
+ }
+
opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") {
options[:debug] = true
}
@@ -192,15 +196,7 @@ def default_options
end
def app
- @app ||= begin
- if !::File.exist? options[:config]
- abort "configuration #{options[:config]} not found"
- end
-
- app, options = Rack::Builder.parse_file(self.options[:config], opt_parser)
- self.options.merge! options
- app
- end
+ @app ||= options[:builder] ? build_app_from_string : build_app_and_options_from_config
end
def self.logging_middleware
@@ -273,6 +269,20 @@ def server
end
private
+ def build_app_and_options_from_config
+ if !::File.exist? options[:config]
+ abort "configuration #{options[:config]} not found"
+ end
+
+ app, options = Rack::Builder.parse_file(self.options[:config], opt_parser)
+ self.options.merge! options
+ app
+ end
+
+ def build_app_from_string
+ Rack::Builder.new_from_string(self.options[:builder])
+ end
+
def parse_options(args)
options = default_options
View
7 test/spec_builder.rb
@@ -204,4 +204,11 @@ def config_file(name)
Rack::MockRequest.new(app).get("/").body.to_s.should.equal '1'
end
end
+
+ describe 'new_from_string' do
+ it "builds a rack app from string" do
+ app, = Rack::Builder.new_from_string "run lambda{|env| [200, {'Content-Type' => 'text/plane'}, ['OK']] }"
+ Rack::MockRequest.new(app).get("/").body.to_s.should.equal 'OK'
+ end
+ end
end
View
6 test/spec_server.rb
@@ -22,6 +22,12 @@ def with_stderr
server.app.should == "FOO"
end
+ should "prefer to use :builder when it is passed in" do
+ server = Rack::Server.new(:builder => "run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['success']] }")
+ server.app.class.should == Proc
+ Rack::MockRequest.new(server.app).get("/").body.to_s.should.equal 'success'
+ end
+
should "not include Rack::Lint in deployment or none environments" do
server = Rack::Server.new(:app => 'foo')
server.middleware['deployment'].flatten.should.not.include(Rack::Lint)

0 comments on commit 7e1f081

Please sign in to comment.