Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/rack/builder.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ def self.parse_file(config, opts = Server::Options.new)
options = opts.parse! $1.split(/\s+/) options = opts.parse! $1.split(/\s+/)
end end
cfgfile.sub!(/^__END__\n.*\Z/m, '') cfgfile.sub!(/^__END__\n.*\Z/m, '')
app = eval "Rack::Builder.new {\n" + cfgfile + "\n}.to_app", app = new_from_string cfgfile, config
TOPLEVEL_BINDING, config, 0
else else
require config require config
app = Object.const_get(::File.basename(config, '.rb').capitalize) app = Object.const_get(::File.basename(config, '.rb').capitalize)
end end
return app, options return app, options
end 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) def initialize(default_app = nil,&block)
@use, @map, @run = [], nil, default_app @use, @map, @run = [], nil, default_app
instance_eval(&block) if block_given? instance_eval(&block) if block_given?
Expand Down
28 changes: 19 additions & 9 deletions lib/rack/server.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def parse!(args)
lineno += 1 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)") { opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") {
options[:debug] = true options[:debug] = true
} }
Expand Down Expand Up @@ -192,15 +196,7 @@ def default_options
end end


def app def app
@app ||= begin @app ||= options[:builder] ? build_app_from_string : 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
end end


def self.logging_middleware def self.logging_middleware
Expand Down Expand Up @@ -273,6 +269,20 @@ def server
end end


private 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) def parse_options(args)
options = default_options options = default_options


Expand Down
7 changes: 7 additions & 0 deletions test/spec_builder.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -204,4 +204,11 @@ def config_file(name)
Rack::MockRequest.new(app).get("/").body.to_s.should.equal '1' Rack::MockRequest.new(app).get("/").body.to_s.should.equal '1'
end end
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 end
6 changes: 6 additions & 0 deletions test/spec_server.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def with_stderr
server.app.should == "FOO" server.app.should == "FOO"
end 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 should "not include Rack::Lint in deployment or none environments" do
server = Rack::Server.new(:app => 'foo') server = Rack::Server.new(:app => 'foo')
server.middleware['deployment'].flatten.should.not.include(Rack::Lint) server.middleware['deployment'].flatten.should.not.include(Rack::Lint)
Expand Down