Skip to content

Commit 4ddc8f1

Browse files
committed
refactor(config): consolidate endpoint configs in runtime
1 parent 1eb0019 commit 4ddc8f1

File tree

4 files changed

+70
-49
lines changed

4 files changed

+70
-49
lines changed

config/dev.exs

-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
import Config
22

3-
# For development, we disable any cache and enable
4-
# debugging and code reloading.
5-
#
6-
# The watchers configuration can be used to run external
7-
# watchers to your application. For example, we use it
8-
# with esbuild to bundle .js and .css sources.
9-
config :epochtalk_server, EpochtalkServerWeb.Endpoint,
10-
# Binding to loopback ipv4 address prevents access from other machines.
11-
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
12-
http: [ip: {0, 0, 0, 0, 0, 0, 0, 0}, port: 4000],
13-
check_origin: false,
14-
code_reloader: true,
15-
debug_errors: false,
16-
secret_key_base: "9ORa6oGSN+xlXNedSn0gIKVc/6//naQqSiZsRJ8vNbcvHpPOTPMLgcn134WIH3Pd",
17-
watchers: []
18-
193
# For Development `config/dev.exs` loads the "Local" adapter which allows preview
204
# of sent emails at the url `/dev/mailbox`. To test SMTP in Development mode,
215
# mailer configurations for adapter, credentials and other options can be

config/runtime.exs

+69-26
Original file line numberDiff line numberDiff line change
@@ -267,33 +267,76 @@ if System.get_env("IMAGES_MODE") == "S3" do
267267
path: System.get_env("S3_PATH", "images/")
268268
end
269269

270-
if config_env() == :prod do
271-
# The secret key base is used to sign/encrypt cookies and other secrets.
272-
# A default value is used in config/dev.exs and config/test.exs but you
273-
# want to use a different value for prod and you most likely don't want
274-
# to check this value into version control, so we use an environment
275-
# variable instead.
276-
secret_key_base =
277-
get_env_or_raise_with_message.(
278-
"SECRET_KEY_BASE",
279-
"You can generate one by calling: mix phx.gen.secret"
280-
)
281-
282-
host = get_env_or_raise.("PHX_HOST")
283-
port = get_env_cast_integer_with_default.("PORT", "4000")
284-
285-
config :epochtalk_server, EpochtalkServerWeb.Endpoint,
286-
url: [host: host, port: 443, scheme: "https"],
287-
http: [
288-
# Enable IPv6 and bind on all interfaces.
289-
# Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
290-
# See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
291-
# for details about using IPv6 vs IPv4 and loopback vs public addresses.
292-
ip: {0, 0, 0, 0, 0, 0, 0, 0},
293-
port: port
294-
],
295-
secret_key_base: secret_key_base
270+
## Configure endpoint
271+
base_endpoint_config = [
272+
url: [host: "localhost"],
273+
render_errors: [
274+
formats: [json: EpochtalkServerWeb.Controllers.ErrorJSON],
275+
layout: false
276+
],
277+
pubsub_server: EpochtalkServer.PubSub,
278+
live_view: [signing_salt: "2Ay27BWv"],
279+
secret_key_base: get_env_or_raise_with_message.(
280+
"SECRET_KEY_BASE",
281+
"""
282+
You can generate one by calling: mix phx.gen.secret
283+
If running in dev mode, add it to .env
284+
"""
285+
)
286+
]
287+
# Secret key base is used to sign/encrypt cookies and other secrets.
288+
endpoint_secret_key_base =
289+
290+
endpoint_config = case config_env() do
291+
:dev ->
292+
# For development, we disable any cache and enable
293+
# debugging and code reloading.
294+
#
295+
# The watchers configuration can be used to run external
296+
# watchers to your application. For example, we use it
297+
# with esbuild to bundle .js and .css sources.
298+
[
299+
# Binding to loopback ipv4 address prevents access from other machines.
300+
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
301+
http: [ip: {0, 0, 0, 0, 0, 0, 0, 0}, port: 4000],
302+
check_origin: false,
303+
code_reloader: true,
304+
debug_errors: false,
305+
watchers: []
306+
]
307+
:test ->
308+
# We don't run a server during test. If one is required,
309+
# you can enable the server option below.
310+
[
311+
http: [ip: {127, 0, 0, 1}, port: 4002],
312+
server: false
313+
]
314+
:prod ->
315+
host = get_env_or_raise.("PHX_HOST")
316+
port = get_env_cast_integer_with_default.("PORT", "4000")
317+
[
318+
# Include the path to a cache manifest
319+
# containing the digested version of static files. This
320+
# manifest is generated by the `mix phx.digest` task,
321+
# which you should run after static files are built and
322+
# before starting your production server.
323+
cache_static_manifest: "priv/static/cache_manifest.json",
324+
url: [host: host, port: 443, scheme: "https"],
325+
http: [
326+
# Enable IPv6 and bind on all interfaces.
327+
# Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
328+
# See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
329+
# for details about using IPv6 vs IPv4 and loopback vs public addresses.
330+
ip: {0, 0, 0, 0, 0, 0, 0, 0},
331+
port: port
332+
]
333+
]
334+
end
335+
336+
config :epochtalk_server, EpochtalkServerWeb.Endpoint,
337+
Keyword.merge(base_endpoint_config, endpoint_config)
296338

339+
if config_env() == :prod do
297340
# ## Configuring the mailer
298341
#
299342
# In production you need to configure the mailer to use a different adapter.

config/test.exs

-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import Config
22

3-
# We don't run a server during test. If one is required,
4-
# you can enable the server option below.
5-
config :epochtalk_server, EpochtalkServerWeb.Endpoint,
6-
http: [ip: {127, 0, 0, 1}, port: 4002],
7-
secret_key_base: "sFacLki12NmjBfy3rJHugf+w+o36b07r99JfjcDAliabs3JdmQ4GAJKlgPVvaaah",
8-
server: false
9-
103
# In test we don't send emails.
114
config :epochtalk_server, EpochtalkServer.Mailer, adapter: Swoosh.Adapters.Test
125

example.env

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
33
AWS_REGION=us-west-2
44
IMAGES_MODE=S3
55
S3_BUCKET=xxxxxxxxxxxxx
6+
SECRET_KEY_BASE=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

0 commit comments

Comments
 (0)