Skip to content

Commit

Permalink
Merge pull request puma#118 from dariocravero/config
Browse files Browse the repository at this point in the history
Added support for setting RACK_ENV through the CLI and the config file.
  • Loading branch information
evanphx committed Jul 19, 2012
2 parents 779e68c + 5c6facd commit f07badc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/puma/cli.rb
Expand Up @@ -52,8 +52,6 @@ def initialize(argv, stdout=STDOUT, stderr=STDERR)
remove.each do |k|
ENV.delete k
end

ENV['RACK_ENV'] ||= "development"
end

def restart_on_stop!
Expand Down Expand Up @@ -213,6 +211,11 @@ def setup_options
"Default: inferred" do |cmd|
@options[:restart_cmd] = cmd
end

o.on "-e", "--environment ENVIRONMENT",
"The environment to run the Rack app on (default development)" do |arg|
@options[:environment] = arg
end
end

@parser.banner = "puma <options> <rackup file>"
Expand All @@ -234,6 +237,11 @@ def write_pid
end
end

def set_rack_environment
# Try the user option first, then the environment variable, finally default to development
ENV['RACK_ENV'] = @options[:environment] || ENV['RACK_ENV'] || 'development'
end

def write_state
require 'yaml'

Expand Down Expand Up @@ -275,6 +283,8 @@ def graceful_stop(server)
def run
parse_options

set_rack_environment

app = @config.app

write_pid
Expand All @@ -289,6 +299,7 @@ def run

log "Puma #{Puma::Const::PUMA_VERSION} starting..."
log "* Min threads: #{min_t}, max threads: #{max_t}"
log "* Environment: #{ENV['RACK_ENV']}"

@options[:binds].each do |str|
uri = URI.parse str
Expand Down
5 changes: 5 additions & 0 deletions lib/puma/configuration.rb
Expand Up @@ -208,6 +208,11 @@ def ssl_bind(host, port, opts)
def state_path(path)
@options[:state] = path.to_s
end

# Set the environment in which the Rack's app will run.
def environment(environment)
@options[:environment] = environment
end
end
end
end
5 changes: 5 additions & 0 deletions lib/rack/handler/puma.rb
Expand Up @@ -18,11 +18,16 @@ def self.run(app, options = {})
app = Rack::CommonLogger.new(app, STDOUT)
end

if options[:environment]
ENV['RACK_ENV'] = options[:environment].to_s
end

server = ::Puma::Server.new(app)
min, max = options[:Threads].split(':', 2)

puts "Puma #{::Puma::Const::PUMA_VERSION} starting..."
puts "* Min threads: #{min}, max threads: #{max}"
puts "* Environment: #{ENV['RACK_ENV']}"
puts "* Listening on tcp://#{options[:Host]}:#{options[:Port]}"

server.add_tcp_listener options[:Host], options[:Port]
Expand Down
9 changes: 9 additions & 0 deletions test/test_cli.rb
Expand Up @@ -5,6 +5,7 @@

class TestCLI < Test::Unit::TestCase
def setup
@environment = 'production'
@tmp_file = Tempfile.new("puma-test")
@tmp_path = @tmp_file.path
@tmp_file.close!
Expand Down Expand Up @@ -156,4 +157,12 @@ def test_load_path
assert_equal 'baz/qux', $LOAD_PATH[0]
$LOAD_PATH.shift
end

def test_environment
cli = Puma::CLI.new ["--environment", @environment]
cli.parse_options
cli.set_rack_environment

assert_equal ENV['RACK_ENV'], @environment
end
end

0 comments on commit f07badc

Please sign in to comment.