diff --git a/config/runtime.exs b/config/runtime.exs index 22fdeb37e43f..fe8ff33802c1 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -246,14 +246,9 @@ data_dir = data_dir || persistent_cache_dir || System.get_env("DEFAULT_DATA_DIR" persistent_cache_dir = persistent_cache_dir || data_dir enable_email_verification = - config_dir - |> get_var_from_path_or_env("ENABLE_EMAIL_VERIFICATION", "false") - |> String.to_existing_atom() + get_bool_from_path_or_env(config_dir, "ENABLE_EMAIL_VERIFICATION", false) -is_selfhost = - config_dir - |> get_var_from_path_or_env("SELFHOST", "true") - |> String.to_existing_atom() +is_selfhost = get_bool_from_path_or_env(config_dir, "SELFHOST", true) # by default, registration is disabled in self-hosted setups disable_registration_default = to_string(is_selfhost) @@ -274,15 +269,10 @@ custom_script_name = config_dir |> get_var_from_path_or_env("CUSTOM_SCRIPT_NAME", "script") -disable_cron = - config_dir - |> get_var_from_path_or_env("DISABLE_CRON", "false") - |> String.to_existing_atom() +disable_cron = get_bool_from_path_or_env(config_dir, "DISABLE_CRON", false) log_failed_login_attempts = - config_dir - |> get_var_from_path_or_env("LOG_FAILED_LOGIN_ATTEMPTS", "false") - |> String.to_existing_atom() + get_bool_from_path_or_env(config_dir, "LOG_FAILED_LOGIN_ATTEMPTS", false) websocket_url = get_var_from_path_or_env(config_dir, "WEBSOCKET_URL", "") @@ -421,7 +411,7 @@ if config_env() in [:ce, :ce_dev, :ce_test] do end db_maybe_ipv6 = - if get_var_from_path_or_env(config_dir, "ECTO_IPV6") do + if get_bool_from_path_or_env(config_dir, "ECTO_IPV6") do if config_env() in [:ce, :ce_dev, :ce_test] do Logger.warning( "ECTO_IPV6 is no longer necessary as all TCP connections now try IPv6 automatically with IPv4 fallback" @@ -522,9 +512,7 @@ config :plausible, Plausible.HelpScout, config :plausible, :imported, max_buffer_size: get_int_from_path_or_env(config_dir, "IMPORTED_MAX_BUFFER_SIZE", 10_000) -maybe_ch_ipv6 = - get_var_from_path_or_env(config_dir, "ECTO_CH_IPV6", "false") - |> String.to_existing_atom() +maybe_ch_ipv6 = get_bool_from_path_or_env(config_dir, "ECTO_CH_IPV6", false) if maybe_ch_ipv6 && config_env() in [:ce, :ce_dev, :ce_test] do Logger.warning( @@ -632,27 +620,22 @@ case mailer_adapter do password: get_var_from_path_or_env(config_dir, "SMTP_USER_PWD"), tls: :if_available, allowed_tls_versions: [:tlsv1, :"tlsv1.1", :"tlsv1.2"], - ssl: get_var_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED") || false, + ssl: get_bool_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED", false), retries: get_var_from_path_or_env(config_dir, "SMTP_RETRIES") || 2, - no_mx_lookups: get_var_from_path_or_env(config_dir, "SMTP_MX_LOOKUPS_ENABLED") || true + no_mx_lookups: get_bool_from_path_or_env(config_dir, "SMTP_MX_LOOKUPS_ENABLED", true) "Bamboo.Mua" -> config :plausible, Plausible.Mailer, adapter: Bamboo.Mua # prevents common problems with Erlang's TLS v1.3 middlebox_comp_mode = - get_var_from_path_or_env(config_dir, "SMTP_MIDDLEBOX_COMP_MODE", "false") + get_bool_from_path_or_env(config_dir, "SMTP_MIDDLEBOX_COMP_MODE", false) - middlebox_comp_mode = String.to_existing_atom(middlebox_comp_mode) config :plausible, Plausible.Mailer, ssl: [middlebox_comp_mode: middlebox_comp_mode] if relay = get_var_from_path_or_env(config_dir, "SMTP_HOST_ADDR") do port = get_int_from_path_or_env(config_dir, "SMTP_HOST_PORT", 587) - - ssl_enabled = - if ssl_enabled = get_var_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED") do - String.to_existing_atom(ssl_enabled) - end + ssl_enabled = get_bool_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED") protocol = cond do @@ -892,10 +875,7 @@ end config :tzdata, :data_dir, Path.join(persistent_cache_dir || System.tmp_dir!(), "tzdata_data") -promex_disabled? = - config_dir - |> get_var_from_path_or_env("PROMEX_DISABLED", "true") - |> String.to_existing_atom() +promex_disabled? = get_bool_from_path_or_env(config_dir, "PROMEX_DISABLED", true) config :plausible, Plausible.PromEx, disabled: promex_disabled?, @@ -922,10 +902,7 @@ if not is_selfhost do config :plausible, Plausible.Site, default_ingest_threshold: site_default_ingest_threshold end -s3_disabled? = - config_dir - |> get_var_from_path_or_env("S3_DISABLED", "true") - |> String.to_existing_atom() +s3_disabled? = get_bool_from_path_or_env(config_dir, "S3_DISABLED", true) unless s3_disabled? do s3_env = [ diff --git a/lib/plausible/helpers/config.ex b/lib/plausible/helpers/config.ex index aa28255d03f4..62853884f4be 100644 --- a/lib/plausible/helpers/config.ex +++ b/lib/plausible/helpers/config.ex @@ -23,4 +23,25 @@ defmodule Plausible.ConfigHelpers do end end end + + def get_bool_from_path_or_env(config_dir, var_name, default \\ nil) do + case get_var_from_path_or_env(config_dir, var_name) do + nil -> default + var -> parse_bool(var) + end + end + + defp parse_bool(var) do + case String.downcase(var) do + t when t in ["1", "t", "true", "y", "yes", "on"] -> + true + + f when f in ["0", "f", "false", "n", "no", "off"] -> + false + + _ -> + raise ArgumentError, + "Invalid boolean value: #{inspect(var)}. Expected one of: 1, 0, t, f, true, false, y, n, on, off" + end + end end