Skip to content

Commit 1ff0e8a

Browse files
committed
Allow rackup .rb files by getting a conventionally named constant as the app
This is written per spec in the rackup binary
1 parent a9acdec commit 1ff0e8a

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

example/myapp.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Myapp = lambda { |env| [200, {}, 'this is my app!'] }

lib/thin/controllers/controller.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,16 @@ def load_adapter
161161
end
162162

163163
def load_rackup_config
164-
rackup_code = File.read(@options[:rackup])
165-
eval("Rack::Builder.new {( #{rackup_code}\n )}.to_app", TOPLEVEL_BINDING, @options[:rackup])
164+
case @options[:rackup]
165+
when /\.rb$/
166+
Kernel.load(@options[:rackup])
167+
Object.const_get(File.basename(@options[:rackup], '.rb').capitalize.to_sym)
168+
when /\.ru$/
169+
rackup_code = File.read(@options[:rackup])
170+
eval("Rack::Builder.new {( #{rackup_code}\n )}.to_app", TOPLEVEL_BINDING, @options[:rackup])
171+
else
172+
raise "Invalid rackup file. please specify either a .ru or .rb file"
173+
end
166174
end
167175
end
168176
end

spec/controllers/controller_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,33 @@
6363
end
6464

6565
it "should load app from Rack config" do
66-
@controller.options[:rackup] = 'example/config.ru'
66+
@controller.options[:rackup] = File.dirname(__FILE__) + '/../../example/config.ru'
6767
@controller.start
6868

6969
@server.app.class.should == Proc
7070
end
71+
72+
it "should load app from ruby file" do
73+
@controller.options[:rackup] = filename = File.dirname(__FILE__) + '/../../example/myapp.rb'
74+
@controller.start
75+
76+
@server.app.should == Myapp
77+
end
78+
79+
it "should throwup if rackup is not a .ru or .rb file" do
80+
proc do
81+
@controller.options[:rackup] = filename = File.dirname(__FILE__) + '/../../example/myapp.foo'
82+
@controller.start
83+
end.should raise_error(RuntimeError, /please/)
84+
end
7185

7286
it "should set server as threaded" do
7387
@controller.options[:threaded] = true
7488
@controller.start
7589

7690
@server.threaded.should be_true
7791
end
92+
7893
end
7994

8095
describe Controller do

0 commit comments

Comments
 (0)