Skip to content

Commit

Permalink
Merge pull request #58 from aschiavon91/chore/improve-hostcfg
Browse files Browse the repository at this point in the history
fix: improve runtime configs
  • Loading branch information
aschiavon91 committed Feb 22, 2024
2 parents afb7055 + f8df844 commit b232a20
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,28 @@ if config_env() == :prod do
port = String.to_integer(System.get_env("PORT", "4000"))
scheme = "http"

uri = %URI{host: host, port: port, scheme: scheme}
uri_wildcard = %URI{host: "*.#{host}", port: port, scheme: scheme}
host_uris = [URI.to_string(uri), URI.to_string(uri_wildcard)]
check_origin = host !== "localhost" && host !== "0.0.0.0" && host !== "127.0.0.1"

cors_hosts =
Enum.map(
[
%URI{host: host, scheme: scheme, port: port},
%URI{host: "*.#{host}", scheme: scheme, port: port}
],
&URI.to_string/1
)

config :harpoon, HarpoonWeb.Endpoint,
url: [host: host || "0.0.0.0", port: port, scheme: scheme],
server: System.get_env("PHX_SERVER", "true") == "true",
check_origin: if(host, do: host_uris, else: false),
check_origin: if(check_origin, do: cors_hosts, else: false),
http: [
ip: {0, 0, 0, 0, 0, 0, 0, 0},
port: port
],
secret_key_base: secret_key_base

config :cors_plug,
origin: if(host, do: host_uris, else: ["*"]),
origin: if(check_origin, do: cors_hosts, else: ["*"]),
max_age: 86_400
end

0 comments on commit b232a20

Please sign in to comment.