Skip to content

Commit 727e788

Browse files
committed
refactor(config/runtime): add corsica config and use in endpoint
1 parent 699b2af commit 727e788

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

config/runtime.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,23 @@ config :epochtalk_server,
366366
EpochtalkServerWeb.Endpoint,
367367
Keyword.merge(base_endpoint_config, endpoint_config)
368368

369+
## Configure corsica
370+
corsica_config =
371+
case config_env() do
372+
:prod ->
373+
get_env_or_raise_with_message.(
374+
"CORS_ORIGINS",
375+
"""
376+
For example:
377+
~r{^https?://(.*\.)?epochtalk\.com$}
378+
"""
379+
)
380+
_ -> "*"
381+
end
382+
383+
config :epochtalk_server, :corsica, corsica_config
384+
385+
369386
## Configure mailer in prod
370387
# (Other envs are hardcoded into their respective config/ files)
371388
if config_env() == :prod do

lib/epochtalk_server_web/endpoint.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ defmodule EpochtalkServerWeb.Endpoint do
66
plug RemoteIp
77

88
# cors configuration
9-
plug Corsica, origins: "*", allow_headers: :all, expose_headers: ["epoch-viewer"]
9+
corsica_config = Application.get_env(:epochtalk_server, :corsica)
10+
plug Corsica,
11+
origins: corsica_config.origins,
12+
allow_headers: :all,
13+
allow_credentials: true,
14+
allow_private_network: true,
15+
expose_headers: ["epoch-viewer", "api-key", "x-api-key"]
1016

1117
socket "/socket", EpochtalkServerWeb.UserSocket,
1218
websocket: true,

0 commit comments

Comments
 (0)