From ee2a68eb5802a12664100c7cca522847cafb6598 Mon Sep 17 00:00:00 2001 From: Jacob Herrington Date: Fri, 17 Sep 2021 18:21:58 -0500 Subject: [PATCH] Add support for APP_ENV environment variable Using APP_ENV to set the environment is supported by Sinatra and Sidekiq, so it makes sense to support the same behavior in Puma. Like Sidekiq, APP_ENV will take precedence over RACK_ENV and RAILS_ENV. APP_ENV defers to any argument passed via the --environment flag. Closes https://github.com/puma/puma/issues/2692 --- README.md | 2 +- lib/puma/configuration.rb | 2 +- lib/puma/control_cli.rb | 2 +- test/test_cli.rb | 13 +++++++++++++ test/test_config.rb | 9 +++++++++ test/test_pumactl.rb | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 62 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ad522cb209..31c3e933f0 100644 --- a/README.md +++ b/README.md @@ -270,7 +270,7 @@ You can also provide a configuration file with the `-C` (or `--config`) flag: $ puma -C /path/to/config ``` -If no configuration file is specified, Puma will look for a configuration file at `config/puma.rb`. If an environment is specified, either via the `-e` and `--environment` flags, or through the `RACK_ENV` or the `RAILS_ENV` environment variables, Puma first looks for configuration at `config/puma/.rb`, and then falls back to `config/puma.rb`. +If no configuration file is specified, Puma will look for a configuration file at `config/puma.rb`. If an environment is specified (via the `--environment` flag or through the `APP_ENV`, `RACK_ENV`, or `RAILS_ENV` environment variables) Puma looks for a configuration file at `config/puma/.rb` and then falls back to `config/puma.rb`. If you want to prevent Puma from looking for a configuration file in those locations, include the `--no-config` flag: diff --git a/lib/puma/configuration.rb b/lib/puma/configuration.rb index c25a891597..9d4a2287b4 100644 --- a/lib/puma/configuration.rb +++ b/lib/puma/configuration.rb @@ -200,7 +200,7 @@ def puma_default_options :worker_shutdown_timeout => DefaultWorkerShutdownTimeout, :remote_address => :socket, :tag => method(:infer_tag), - :environment => -> { ENV['RACK_ENV'] || ENV['RAILS_ENV'] || "development" }, + :environment => -> { ENV['APP_ENV'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development' }, :rackup => DefaultRackup, :logger => STDOUT, :persistent_timeout => Const::PERSISTENT_TIMEOUT, diff --git a/lib/puma/control_cli.rb b/lib/puma/control_cli.rb index dcf1f0eadc..efa3a86ea1 100644 --- a/lib/puma/control_cli.rb +++ b/lib/puma/control_cli.rb @@ -47,7 +47,7 @@ def initialize(argv, stdout=STDOUT, stderr=STDERR) @control_auth_token = nil @config_file = nil @command = nil - @environment = ENV['RACK_ENV'] || ENV['RAILS_ENV'] + @environment = ENV['APP_ENV'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] @argv = argv.dup @stdout = stdout diff --git a/test/test_cli.rb b/test/test_cli.rb index c175be91b2..1662479a3f 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -426,6 +426,19 @@ def test_extra_runtime_dependencies assert_equal %w[a b], extra_dependencies end + def test_environment_app_env + ENV['RACK_ENV'] = @environment + ENV['RAILS_ENV'] = @environment + ENV['APP_ENV'] = 'test' + + Puma::CLI.new [] + + assert_equal 'test', ENV['RACK_ENV'] + + ENV.delete 'APP_ENV' + ENV.delete 'RAILS_ENV' + end + def test_environment_rack_env ENV.delete 'RACK_ENV' diff --git a/test/test_config.rb b/test/test_config.rb index 2052cc7079..9ba564653b 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -466,6 +466,15 @@ def setup File.write("config/puma/fake-env.rb", "") end + def test_config_files_with_app_env + with_env('APP_ENV' => 'fake-env') do + conf = Puma::Configuration.new do + end + + assert_equal ['config/puma/fake-env.rb'], conf.config_files + end + end + def test_config_files_with_rack_env with_env('RACK_ENV' => 'fake-env') do conf = Puma::Configuration.new do diff --git a/test/test_pumactl.rb b/test/test_pumactl.rb index dba2dca61b..0181add8fb 100644 --- a/test/test_pumactl.rb +++ b/test/test_pumactl.rb @@ -46,6 +46,13 @@ def test_config_file assert_equal "t3-pid", control_cli.instance_variable_get("@pidfile") end + def test_app_env_without_environment + with_env('APP_ENV' => 'test') do + control_cli = Puma::ControlCLI.new ['halt'] + assert_equal 'test', control_cli.instance_variable_get('@environment') + end + end + def test_rack_env_without_environment with_env("RACK_ENV" => "test") do control_cli = Puma::ControlCLI.new ["halt"] @@ -53,6 +60,36 @@ def test_rack_env_without_environment end end + def test_app_env_precedence + with_env('APP_ENV' => nil, 'RACK_ENV' => nil, 'RAILS_ENV' => 'production') do + control_cli = Puma::ControlCLI.new ['halt'] + assert_equal 'production', control_cli.instance_variable_get('@environment') + end + + with_env('APP_ENV' => nil, 'RACK_ENV' => 'test', 'RAILS_ENV' => 'production') do + control_cli = Puma::ControlCLI.new ['halt'] + assert_equal 'test', control_cli.instance_variable_get('@environment') + end + + with_env('APP_ENV' => 'development', 'RACK_ENV' => 'test', 'RAILS_ENV' => 'production') do + control_cli = Puma::ControlCLI.new ['halt'] + assert_equal 'development', control_cli.instance_variable_get('@environment') + + control_cli = Puma::ControlCLI.new ['-e', 'test', 'halt'] + assert_equal 'test', control_cli.instance_variable_get('@environment') + end + end + + def test_environment_without_app_env + with_env('APP_ENV' => nil, 'RACK_ENV' => nil, 'RAILS_ENV' => nil) do + control_cli = Puma::ControlCLI.new ['halt'] + assert_nil control_cli.instance_variable_get('@environment') + + control_cli = Puma::ControlCLI.new ['-e', 'test', 'halt'] + assert_equal 'test', control_cli.instance_variable_get('@environment') + end + end + def test_environment_without_rack_env with_env("RACK_ENV" => nil, 'RAILS_ENV' => nil) do control_cli = Puma::ControlCLI.new ["halt"]