Skip to content

Commit 9710248

Browse files
committed
Support PORT env var in dev using runtime.exs
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.
1 parent 5cd3930 commit 9710248

File tree

7 files changed

+27
-9
lines changed

7 files changed

+27
-9
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ if System.get_env("PHX_SERVER") do
2020
config :<%= @app_name %>, <%= @endpoint_module %>, server: true
2121
end
2222

23+
port = String.to_integer(System.get_env("PORT", "4000"))
24+
25+
if config_env() == :dev do
26+
config :<%= @app_name %>, <%= @endpoint_module %>, http: [port: port]
27+
end
28+
2329
if config_env() == :prod do
2430
# The secret key base is used to sign/encrypt cookies and other secrets.
2531
# A default value is used in config/dev.exs and config/test.exs but you
@@ -34,7 +40,6 @@ if config_env() == :prod do
3440
"""
3541

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

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

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ config :<%= @web_app_name %>, <%= @endpoint_module %>,
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.
1717
ip: {0, 0, 0, 0, 0, 0, 0, 0},
18-
port: String.to_integer(System.get_env("PORT") || "4000")
18+
port: port
1919
],
2020
secret_key_base: secret_key_base
2121

installer/templates/phx_umbrella/config/runtime.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ 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+
port = String.to_integer(System.get_env("PORT", "4000"))
11+
12+
if config_env() == :dev do
13+
config :<%= @app_name %>, <%= @endpoint_module %>, http: [port: port]
14+
end
15+
916
if config_env() == :prod do
1017
config :<%= @app_name %>, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY")
1118
end

installer/test/phx_new_test.exs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ defmodule Mix.Tasks.Phx.NewTest do
3131
Mix.Tasks.Phx.New.run([@app_name])
3232

3333
assert_file("phx_blog/README.md")
34+
3435
assert_file("phx_blog/AGENTS.md", fn file ->
3536
assert file =~ "### UI/UX & design guidelines"
3637
end)
@@ -63,7 +64,12 @@ defmodule Mix.Tasks.Phx.NewTest do
6364
assert file =~ "config :logger, level: :info"
6465
end)
6566

66-
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 =~ ~r/^port = String.to_integer\(System.get_env\("PORT", "4000"\)\)$/
69+
assert file =~ ~r/^config :phx_blog, PhxBlogWeb.Endpoint, http: \[port: port\]$/
70+
assert file =~ ~r/^\s+ip: {0, 0, 0, 0, 0, 0, 0, 0},$/
71+
assert file =~ ~r/^\s+port: port$/
72+
end)
6773

6874
assert_file("phx_blog/lib/phx_blog/application.ex", ~r/defmodule PhxBlog.Application do/)
6975
assert_file("phx_blog/lib/phx_blog.ex", ~r/defmodule PhxBlog do/)
@@ -145,7 +151,7 @@ defmodule Mix.Tasks.Phx.NewTest do
145151
assert_file("phx_blog/config/dev.exs", fn file ->
146152
assert file =~ "esbuild: {Esbuild,"
147153
assert file =~ "lib/phx_blog_web/(?:controllers|live|components|router)/?.*\\.(ex|heex)$"
148-
assert file =~ "http: [ip: {127, 0, 0, 1}"
154+
assert file =~ "http: [ip: {127, 0, 0, 1}]"
149155
end)
150156

151157
# 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)