diff --git a/installer/templates/phx_single/config/dev.exs b/installer/templates/phx_single/config/dev.exs index 95a5fa1fe4..482e8176a7 100644 --- a/installer/templates/phx_single/config/dev.exs +++ b/installer/templates/phx_single/config/dev.exs @@ -10,10 +10,10 @@ config :<%= @app_name %>, <%= @endpoint_module %>,<%= if @inside_docker_env? do # Bind to 0.0.0.0 to expose the server to the docker host machine. # This makes make the service accessible from any network interface. # Change to `ip: {127, 0, 0, 1}` to allow access only from the server machine. - http: [ip: {0, 0, 0, 0}, port: String.to_integer(System.get_env("PORT") || "4000")],<% else %> + http: [ip: {0, 0, 0, 0}],<% else %> # Binding to loopback ipv4 address prevents access from other machines. # Change to `ip: {0, 0, 0, 0}` to allow access from other machines. - http: [ip: {127, 0, 0, 1}, port: String.to_integer(System.get_env("PORT") || "4000")],<% end %> + http: [ip: {127, 0, 0, 1}],<% end %> check_origin: false, code_reloader: true, debug_errors: true, diff --git a/installer/templates/phx_single/config/runtime.exs b/installer/templates/phx_single/config/runtime.exs index 7f2d1d608d..675e0feb8c 100644 --- a/installer/templates/phx_single/config/runtime.exs +++ b/installer/templates/phx_single/config/runtime.exs @@ -20,6 +20,9 @@ if System.get_env("PHX_SERVER") do config :<%= @app_name %>, <%= @endpoint_module %>, server: true end +config :<%= @app_name %>, <%= @endpoint_module %>, + http: [port: String.to_integer(System.get_env("PORT", "4000"))] + if config_env() == :prod do # The secret key base is used to sign/encrypt cookies and other secrets. # A default value is used in config/dev.exs and config/test.exs but you @@ -34,7 +37,6 @@ if config_env() == :prod do """ host = System.get_env("PHX_HOST") || "example.com" - port = String.to_integer(System.get_env("PORT") || "4000") config :<%= @app_name %>, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY") @@ -45,8 +47,7 @@ if config_env() == :prod do # Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access. # See the documentation on <%= @web_adapter_docs %> # for details about using IPv6 vs IPv4 and loopback vs public addresses. - ip: {0, 0, 0, 0, 0, 0, 0, 0}, - port: port + ip: {0, 0, 0, 0, 0, 0, 0, 0} ], secret_key_base: secret_key_base diff --git a/installer/templates/phx_umbrella/apps/app_name_web/config/dev.exs b/installer/templates/phx_umbrella/apps/app_name_web/config/dev.exs index 8cecdd2bb0..6a044c4131 100644 --- a/installer/templates/phx_umbrella/apps/app_name_web/config/dev.exs +++ b/installer/templates/phx_umbrella/apps/app_name_web/config/dev.exs @@ -10,10 +10,10 @@ config :<%= @web_app_name %>, <%= @endpoint_module %>,<%= if @inside_docker_env? # Bind to 0.0.0.0 to expose the server to the docker host machine. # This makes make the service accessible from any network interface. # Change to `ip: {127, 0, 0, 1}` to allow access only from the server machine. - http: [ip: {0, 0, 0, 0}, port: String.to_integer(System.get_env("PORT") || "4000")],<% else %> + http: [ip: {0, 0, 0, 0}],<% else %> # Binding to loopback ipv4 address prevents access from other machines. # Change to `ip: {0, 0, 0, 0}` to allow access from other machines. - http: [ip: {127, 0, 0, 1}, port: String.to_integer(System.get_env("PORT") || "4000")],<% end %> + http: [ip: {127, 0, 0, 1}],<% end %> check_origin: false, code_reloader: true, debug_errors: true, diff --git a/installer/templates/phx_umbrella/apps/app_name_web/config/runtime.exs b/installer/templates/phx_umbrella/apps/app_name_web/config/runtime.exs index e32393f177..8b62c9db4c 100644 --- a/installer/templates/phx_umbrella/apps/app_name_web/config/runtime.exs +++ b/installer/templates/phx_umbrella/apps/app_name_web/config/runtime.exs @@ -14,8 +14,7 @@ config :<%= @web_app_name %>, <%= @endpoint_module %>, http: [ # Enable IPv6 and bind on all interfaces. # Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access. - ip: {0, 0, 0, 0, 0, 0, 0, 0}, - port: String.to_integer(System.get_env("PORT") || "4000") + ip: {0, 0, 0, 0, 0, 0, 0, 0} ], secret_key_base: secret_key_base diff --git a/installer/templates/phx_umbrella/config/runtime.exs b/installer/templates/phx_umbrella/config/runtime.exs index 587a07f8a7..6e56791aa0 100644 --- a/installer/templates/phx_umbrella/config/runtime.exs +++ b/installer/templates/phx_umbrella/config/runtime.exs @@ -6,6 +6,10 @@ import Config # and secrets from environment variables or elsewhere. Do not define # any compile-time configuration in here, as it won't be applied. # The block below contains prod specific runtime configuration. + +config :<%= @app_name %>, <%= @endpoint_module %>, + http: [port: String.to_integer(System.get_env("PORT", "4000"))] + if config_env() == :prod do config :<%= @app_name %>, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY") end diff --git a/installer/test/phx_new_test.exs b/installer/test/phx_new_test.exs index c1c1166f62..7df19fce61 100644 --- a/installer/test/phx_new_test.exs +++ b/installer/test/phx_new_test.exs @@ -64,7 +64,12 @@ defmodule Mix.Tasks.Phx.NewTest do assert file =~ "config :logger, level: :info" end) - assert_file("phx_blog/config/runtime.exs", ~r/ip: {0, 0, 0, 0, 0, 0, 0, 0}/) + assert_file("phx_blog/config/runtime.exs", fn file -> + assert file =~ + ~r/^ http: \[port: String.to_integer\(System.get_env\("PORT", "4000"\)\)\]$/m + + assert file =~ ~r/^\s+ip: {0, 0, 0, 0, 0, 0, 0, 0}$/m + end) assert_file("phx_blog/lib/phx_blog/application.ex", ~r/defmodule PhxBlog.Application do/) assert_file("phx_blog/lib/phx_blog.ex", ~r/defmodule PhxBlog do/) @@ -147,7 +152,7 @@ defmodule Mix.Tasks.Phx.NewTest do assert file =~ "esbuild: {Esbuild," assert file =~ "lib/phx_blog_web/router\\.ex$" assert file =~ "lib/phx_blog_web/(controllers|live|components)/.*\\.(ex|heex)$" - assert file =~ "http: [ip: {127, 0, 0, 1}" + assert file =~ "http: [ip: {127, 0, 0, 1}]" end) # tailwind diff --git a/lib/mix/tasks/phx.gen.cert.ex b/lib/mix/tasks/phx.gen.cert.ex index 46d1f44404..232bf1e486 100644 --- a/lib/mix/tasks/phx.gen.cert.ex +++ b/lib/mix/tasks/phx.gen.cert.ex @@ -122,7 +122,7 @@ defmodule Mix.Tasks.Phx.Gen.Cert do configuration in config/dev.exs: config #{inspect(app)}, #{inspect(Mix.Phoenix.web_module(base))}.Endpoint, - http: [port: String.to_integer(System.get_env("PORT") || "4000")], + ..., https: [ port: 4001, cipher_suite: :strong,