Skip to content

Commit 2ea807d

Browse files
authored
Support PORT env var using runtime.exs (#6449)
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 feef607.
1 parent 380a86a commit 2ea807d

File tree

7 files changed

+21
-12
lines changed

7 files changed

+21
-12
lines changed

installer/templates/phx_single/config/dev.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ config :<%= @app_name %>, <%= @endpoint_module %>,<%= if @inside_docker_env? do
1010
# Bind to 0.0.0.0 to expose the server to the docker host machine.
1111
# This makes make the service accessible from any network interface.
1212
# Change to `ip: {127, 0, 0, 1}` to allow access only from the server machine.
13-
http: [ip: {0, 0, 0, 0}, port: String.to_integer(System.get_env("PORT") || "4000")],<% else %>
13+
http: [ip: {0, 0, 0, 0}],<% else %>
1414
# Binding to loopback ipv4 address prevents access from other machines.
1515
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
16-
http: [ip: {127, 0, 0, 1}, port: String.to_integer(System.get_env("PORT") || "4000")],<% end %>
16+
http: [ip: {127, 0, 0, 1}],<% end %>
1717
check_origin: false,
1818
code_reloader: true,
1919
debug_errors: true,

installer/templates/phx_single/config/runtime.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ if System.get_env("PHX_SERVER") do
2020
config :<%= @app_name %>, <%= @endpoint_module %>, server: true
2121
end
2222

23+
config :<%= @app_name %>, <%= @endpoint_module %>,
24+
http: [port: String.to_integer(System.get_env("PORT", "4000"))]
25+
2326
if config_env() == :prod do
2427
# The secret key base is used to sign/encrypt cookies and other secrets.
2528
# A default value is used in config/dev.exs and config/test.exs but you
@@ -34,7 +37,6 @@ if config_env() == :prod do
3437
"""
3538

3639
host = System.get_env("PHX_HOST") || "example.com"
37-
port = String.to_integer(System.get_env("PORT") || "4000")
3840

3941
config :<%= @app_name %>, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY")
4042

@@ -45,8 +47,7 @@ if config_env() == :prod do
4547
# Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
4648
# See the documentation on <%= @web_adapter_docs %>
4749
# for details about using IPv6 vs IPv4 and loopback vs public addresses.
48-
ip: {0, 0, 0, 0, 0, 0, 0, 0},
49-
port: port
50+
ip: {0, 0, 0, 0, 0, 0, 0, 0}
5051
],
5152
secret_key_base: secret_key_base
5253

installer/templates/phx_umbrella/apps/app_name_web/config/dev.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ config :<%= @web_app_name %>, <%= @endpoint_module %>,<%= if @inside_docker_env?
1010
# Bind to 0.0.0.0 to expose the server to the docker host machine.
1111
# This makes make the service accessible from any network interface.
1212
# Change to `ip: {127, 0, 0, 1}` to allow access only from the server machine.
13-
http: [ip: {0, 0, 0, 0}, port: String.to_integer(System.get_env("PORT") || "4000")],<% else %>
13+
http: [ip: {0, 0, 0, 0}],<% else %>
1414
# Binding to loopback ipv4 address prevents access from other machines.
1515
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
16-
http: [ip: {127, 0, 0, 1}, port: String.to_integer(System.get_env("PORT") || "4000")],<% end %>
16+
http: [ip: {127, 0, 0, 1}],<% end %>
1717
check_origin: false,
1818
code_reloader: true,
1919
debug_errors: true,

installer/templates/phx_umbrella/apps/app_name_web/config/runtime.exs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ config :<%= @web_app_name %>, <%= @endpoint_module %>,
1414
http: [
1515
# Enable IPv6 and bind on all interfaces.
1616
# Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
17-
ip: {0, 0, 0, 0, 0, 0, 0, 0},
18-
port: String.to_integer(System.get_env("PORT") || "4000")
17+
ip: {0, 0, 0, 0, 0, 0, 0, 0}
1918
],
2019
secret_key_base: secret_key_base
2120

installer/templates/phx_umbrella/config/runtime.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import Config
66
# and secrets from environment variables or elsewhere. Do not define
77
# any compile-time configuration in here, as it won't be applied.
88
# The block below contains prod specific runtime configuration.
9+
10+
config :<%= @app_name %>, <%= @endpoint_module %>,
11+
http: [port: String.to_integer(System.get_env("PORT", "4000"))]
12+
913
if config_env() == :prod do
1014
config :<%= @app_name %>, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY")
1115
end

installer/test/phx_new_test.exs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ defmodule Mix.Tasks.Phx.NewTest do
6464
assert file =~ "config :logger, level: :info"
6565
end)
6666

67-
assert_file("phx_blog/config/runtime.exs", ~r/ip: {0, 0, 0, 0, 0, 0, 0, 0}/)
67+
assert_file("phx_blog/config/runtime.exs", fn file ->
68+
assert file =~
69+
~r/^ http: \[port: String.to_integer\(System.get_env\("PORT", "4000"\)\)\]$/m
70+
71+
assert file =~ ~r/^\s+ip: {0, 0, 0, 0, 0, 0, 0, 0}$/m
72+
end)
6873

6974
assert_file("phx_blog/lib/phx_blog/application.ex", ~r/defmodule PhxBlog.Application do/)
7075
assert_file("phx_blog/lib/phx_blog.ex", ~r/defmodule PhxBlog do/)
@@ -147,7 +152,7 @@ defmodule Mix.Tasks.Phx.NewTest do
147152
assert file =~ "esbuild: {Esbuild,"
148153
assert file =~ "lib/phx_blog_web/router\\.ex$"
149154
assert file =~ "lib/phx_blog_web/(controllers|live|components)/.*\\.(ex|heex)$"
150-
assert file =~ "http: [ip: {127, 0, 0, 1}"
155+
assert file =~ "http: [ip: {127, 0, 0, 1}]"
151156
end)
152157

153158
# tailwind

lib/mix/tasks/phx.gen.cert.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ defmodule Mix.Tasks.Phx.Gen.Cert do
122122
configuration in config/dev.exs:
123123
124124
config #{inspect(app)}, #{inspect(Mix.Phoenix.web_module(base))}.Endpoint,
125-
http: [port: String.to_integer(System.get_env("PORT") || "4000")],
125+
...,
126126
https: [
127127
port: 4001,
128128
cipher_suite: :strong,

0 commit comments

Comments
 (0)