Skip to content

Commit

Permalink
refactor(config/runtime): add corsica config and use in endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
unenglishable committed Jan 29, 2025
1 parent 699b2af commit 727e788
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
17 changes: 17 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,23 @@ config :epochtalk_server,
EpochtalkServerWeb.Endpoint,
Keyword.merge(base_endpoint_config, endpoint_config)

## Configure corsica
corsica_config =
case config_env() do
:prod ->
get_env_or_raise_with_message.(
"CORS_ORIGINS",
"""
For example:
~r{^https?://(.*\.)?epochtalk\.com$}
"""
)
_ -> "*"
end

config :epochtalk_server, :corsica, corsica_config


## Configure mailer in prod
# (Other envs are hardcoded into their respective config/ files)
if config_env() == :prod do
Expand Down
8 changes: 7 additions & 1 deletion lib/epochtalk_server_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ defmodule EpochtalkServerWeb.Endpoint do
plug RemoteIp

# cors configuration
plug Corsica, origins: "*", allow_headers: :all, expose_headers: ["epoch-viewer"]
corsica_config = Application.get_env(:epochtalk_server, :corsica)
plug Corsica,
origins: corsica_config.origins,
allow_headers: :all,
allow_credentials: true,
allow_private_network: true,
expose_headers: ["epoch-viewer", "api-key", "x-api-key"]

socket "/socket", EpochtalkServerWeb.UserSocket,
websocket: true,
Expand Down

0 comments on commit 727e788

Please sign in to comment.