Skip to content

Commit

Permalink
move default (static) rack env generation into config.rack_env
Browse files Browse the repository at this point in the history
  • Loading branch information
jakesgordon committed Aug 23, 2014
1 parent 7435503 commit 25cb984
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
17 changes: 17 additions & 0 deletions lib/rack-rabbit/config.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'logger'
require 'rack'

require 'rack-rabbit'

Expand All @@ -14,6 +15,7 @@ def initialize(options = {})
end

def reload(options = {})
@rack_env = nil
instance_eval(File.read(config_file), config_file) if config_file && File.exists?(config_file)
validate(options) unless options[:validate] == false
end
Expand Down Expand Up @@ -182,6 +184,21 @@ def after_fork(server=nil, worker=nil, &block)

#--------------------------------------------------------------------------

def rack_env
@rack_env ||= {
'rack.version' => Rack::VERSION,
'rack.logger' => logger,
'rack.errors' => $stderr,
'rack.multithread' => false,
'rack.multiprocess' => true,
'rack.run_once' => false,
'rack.url_scheme' => 'http',
'SERVER_NAME' => app_id
}
end

#--------------------------------------------------------------------------

private

attr_reader :values
Expand Down
18 changes: 1 addition & 17 deletions lib/rack-rabbit/handler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'rack'
require 'stringio'

require 'rack-rabbit/response'
Expand Down Expand Up @@ -94,7 +93,7 @@ def handle(message)

def build_env(message)

default_env.merge({
config.rack_env.merge({
'rabbit.message' => message,
'rack.input' => StringIO.new(message.body || ""),
'REQUEST_METHOD' => message.method,
Expand All @@ -109,21 +108,6 @@ def build_env(message)

#--------------------------------------------------------------------------

def default_env
@default_env ||= {
'rack.version' => Rack::VERSION,
'rack.logger' => logger,
'rack.errors' => $stderr,
'rack.multithread' => false,
'rack.multiprocess' => true,
'rack.run_once' => false,
'rack.url_scheme' => 'http',
'SERVER_NAME' => config.app_id
}
end

#--------------------------------------------------------------------------

def response_properties(message, response)
return {
:app_id => config.app_id,
Expand Down
14 changes: 14 additions & 0 deletions test/unit/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,20 @@ def test_after_fork
assert_equal(:worker, worker, "verify block got called")
end

#--------------------------------------------------------------------------

def test_rack_env
config = build_config(:app_id => APP_ID)
assert_equal(Rack::VERSION, config.rack_env['rack.version'])
assert_equal(config.logger, config.rack_env['rack.logger'])
assert_equal($stderr, config.rack_env['rack.errors'])
assert_equal(false, config.rack_env['rack.multithread'])
assert_equal(true, config.rack_env['rack.multiprocess'])
assert_equal(false, config.rack_env['rack.run_once'])
assert_equal('http', config.rack_env['rack.url_scheme'])
assert_equal(APP_ID, config.rack_env['SERVER_NAME'])
end

#==========================================================================
# private helper methods
#==========================================================================
Expand Down

0 comments on commit 25cb984

Please sign in to comment.