Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add a rackup option for a one-liner rack app server #480

Merged
merged 2 commits into from

2 participants

@udzura

Hi, Rack team,

I have created a rackup option -b/--builder, which is passed with Rack::Builder's build script.

This patch allows you a command like below, and will boot up the file directory server, without any config.ru file.

bundle exec rackup -b "run Rack::Directory.new '.'"

I intend this option to be just used as plackup's one-liner option -e.

How do you think?

Thanks,
KONDO Uchio

@raggi raggi merged commit 7e1f081 into rack:master
@raggi
Owner

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
8 lib/rack/builder.rb
@@ -37,8 +37,7 @@ 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)
@@ -46,6 +45,11 @@ def self.parse_file(config, opts = Server::Options.new)
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)
Something went wrong with that request. Please try again.