-
Notifications
You must be signed in to change notification settings - Fork 3k
Support PORT env var using runtime.exs
#6449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support PORT env var using runtime.exs
#6449
Conversation
installer/templates/phx_umbrella/apps/app_name_web/config/runtime.exs
Outdated
Show resolved
Hide resolved
9710248
to
8ed466b
Compare
Hmm the failed test passes for me locally: $ mix test test/phoenix/integration/endpoint_test.exs:126
Compiling 74 files (.ex)
Generated phoenix app
Running ExUnit with seed: 996645, max_cases: 16
Excluding tags: [:test]
Including tags: [location: {"test/phoenix/integration/endpoint_test.exs", 126}]
.
Finished in 1.1 seconds (0.00s async, 1.1s sync)
4 tests, 0 failures, 3 excluded |
@rhcarvalho I did not experience any issues with having it in the build-time config for dev, did you? |
No particular issue. Maybe it's my limited knowledge :) My first instinct when seeing the new code in When Mix is available, I don't remember where exactly I got my "lesson" from, but found a few related threads in the forum:
And I was about to close this PR until I found two interesting historical commits:
$ git show e8d9b45c32989377726da21075f85f1f3866a837:priv/template/config/dev.exs
use Mix.Config
config :<%= application_name %>, <%= application_module %>.Endpoint,
http: [port: System.get_env("PORT") || 4000],
debug_errors: true,
cache_static_lookup: false
# Enables code reloading for development
config :phoenix, :code_reloader, true
$ git show e8d9b45c32989377726da21075f85f1f3866a837
commit e8d9b45c32989377726da21075f85f1f3866a837
Author: José Valim <[email protected]>
Date: Sat Dec 13 14:25:38 2014 +0100
Remove repeated HTTP config
diff --git a/priv/template/config/config.exs b/priv/template/config/config.exs
index 3932e08cd..eec091a6d 100644
--- a/priv/template/config/config.exs
+++ b/priv/template/config/config.exs
@@ -8,7 +8,6 @@ use Mix.Config
# Configures the endpoint
config :<%= application_name %>, <%= application_module %>.Endpoint,
url: [host: "localhost"],
- http: [port: System.get_env("PORT")],
secret_key_base: "<%= secret_key_base %>",
debug_errors: false Note that the
Quoting José's commit message:
And then My intuition and motivation for the change matched exactly what José wrote back then. I'm guessing Chris reintroduced the configurable port in dev for something like @SteffenDE what do you think, shall we drop this? Or is it still "teaching the right patterns" and worth doing again nowadays? |
Yeah, I think @josevalim was / is right about not teaching about if port = System.get_env("PORT") do
config :my_app, MyAppWeb.Endpoint, http: [port: String.to_integer(port)]
end and not check the config_env at all for the port. |
Good call, makes it work the same regardless of env. We'd be potentially affecting the standard I can update the PR. |
While I edit the code, I see one downside to globally setting the port: endpoint config becomes spread out, making the setting of IP and port far apart in the config file, which can cause friction for humans trying to update the config or reason about what is set and why. This concern might not be real. |
The config/dev.exs file is for build time configuration. This commit updates the support for the PORT environment variable in new projects generated with `phx.new` to be done in runtime.exs instead. The HTTP port is configured regardless of the environment, making it easier to reason about. This change is motivated by a desire to not promote the use of `System.get_env` in config files other than `runtime.exs`, since the Phoenix templates are often referenced and expected to "teach the right patterns". See also phoenixframework@feef607.
8ed466b
to
5dc9576
Compare
Rebased on top of latest main and applied your suggestion @SteffenDE, indeed applying the same config to all environments makes the overall config less repetitive. |
runtime.exs
runtime.exs
https://github.com/phoenixframework/phoenix/actions/runs/17941936145/job/51020424195?pr=6449 Test error seems to be due to a Docker Hub timeout. |
yeah Docker Hub isn't too reliable... |
💚 💙 💜 💛 ❤️ |
While upgrading an app from v1.7.21 to v1.8.1, I noticed changes to
config/dev.exs
in new projects which depended on the system environment at build time.This commit updates the support for the
PORT
environment variable in new projects generated withphx.new
to be done inruntime.exs
instead ofdev.exs
, which is meant for static build time config.Dev support for PORT was added a few months ago in 7a88d0f, and shipped first in v1.8.0-rc.1.