Skip to content

Commit 4cc1c14

Browse files
authored
Merge pull request rails#28196 from y-yagi/set_correct_host_except_development_environment
Set correct host except development environment
2 parents 0bd325e + 60aeb6a commit 4cc1c14

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

railties/lib/rails/commands/server/server_command.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ class ServerCommand < Base # :nodoc:
9999

100100
class_option :port, aliases: "-p", type: :numeric,
101101
desc: "Runs Rails on the specified port.", banner: :port, default: 3000
102-
class_option :binding, aliases: "-b", type: :string, default: "localhost",
103-
desc: "Binds Rails to the specified IP.", banner: :IP
102+
class_option :binding, aliases: "-b", type: :string,
103+
desc: "Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'.",
104+
banner: :IP
104105
class_option :config, aliases: "-c", type: :string, default: "config.ru",
105106
desc: "Uses a custom rackup configuration.", banner: :file
106107
class_option :daemon, aliases: "-d", type: :boolean, default: false,
@@ -187,7 +188,10 @@ def port
187188
end
188189

189190
def host
190-
ENV.fetch("HOST", options[:binding])
191+
unless (default_host = options[:binding])
192+
default_host = environment == "development" ? "localhost" : "0.0.0.0"
193+
end
194+
ENV.fetch("HOST", default_host)
191195
end
192196

193197
def environment

railties/test/commands/server_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ def test_log_stdout
121121
end
122122
end
123123

124+
def test_host
125+
with_rails_env "development" do
126+
options = parse_arguments([])
127+
assert_equal "localhost", options[:Host]
128+
end
129+
130+
with_rails_env "production" do
131+
options = parse_arguments([])
132+
assert_equal "0.0.0.0", options[:Host]
133+
end
134+
135+
with_rails_env "development" do
136+
args = ["-b", "127.0.0.1"]
137+
options = parse_arguments(args)
138+
assert_equal "127.0.0.1", options[:Host]
139+
end
140+
end
141+
124142
def test_records_user_supplied_options
125143
server_options = parse_arguments(["-p", 3001])
126144
assert_equal [:Port], server_options[:user_supplied_options]

0 commit comments

Comments
 (0)