1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

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
This commit is contained in:
Jacob Herrington 2021-09-17 18:21:58 -05:00
parent b9fddb90d2
commit ff6f08c87a
6 changed files with 62 additions and 3 deletions

View file

@ -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/<environment_name>.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/<environment_name>.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:

View file

@ -200,7 +200,7 @@ module Puma
: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,

View file

@ -47,7 +47,7 @@ module Puma
@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

View file

@ -426,6 +426,19 @@ class TestCLI < Minitest::Test
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'
cli = Puma::CLI.new []
assert_equal 'test', cli.environment
ENV.delete 'APP_ENV'
ENV.delete 'RAILS_ENV'
end
def test_environment_rack_env
ENV.delete 'RACK_ENV'

View file

@ -466,6 +466,15 @@ class TestConfigFileWithFakeEnv < TestConfigFileBase
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

View file

@ -46,6 +46,13 @@ class TestPumaControlCli < TestConfigFileBase
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 @@ class TestPumaControlCli < TestConfigFileBase
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"]