PhoenixKit.Config (phoenix_kit v1.6.16)
View SourceConfiguration management system for PhoenixKit.
This module provides a centralized way to manage PhoenixKit configuration with type-safe getter functions for different data types.
Usage
# Get all configuration
config = PhoenixKit.Config.get_all()
# Get specific values
repo = PhoenixKit.Config.get(:repo)
mailer = PhoenixKit.Config.get(:mailer, PhoenixKit.Mailer)
# Type-safe getters
options = PhoenixKit.Config.get_list(:options, [])
enabled = PhoenixKit.Config.get_boolean(:enabled, false)
host = PhoenixKit.Config.get_string(:host, "localhost")Configuration Keys
:repo- Ecto repository module (required):mailer- Mailer module for sending emails:host- Application hostname:port- Application port:layout_module- Custom layout configuration:from_email- Default sender email address for notifications:from_name- Default sender name for notifications (default: "PhoenixKit"):users_module- User schema module (default: PhoenixKit.Users.Auth.User)
Type-Safe Functions
get_list/2- Gets configuration values with list type validationget_boolean/2- Gets configuration values with boolean type validationget_string/2- Gets configuration values with string type validation
These functions provide automatic type validation and fallback to defaults when the configuration value is missing or has the wrong type.
Summary
Functions
Gets a specific configuration value.
Gets a specific configuration value with a default.
Gets all PhoenixKit configuration.
Gets configured host with an optional port or default value.
Gets a configuration value as a boolean with type validation.
Gets the base URL dynamically from the parent Phoenix Endpoint if available, otherwise falls back to the static configuration.
Gets a configuration value as a list with type validation.
Gets the configured mailer module.
Gets the parent application name that is using PhoenixKit.
Gets configuration from the parent application.
Gets the parent application's Phoenix Endpoint module.
Gets the parent Phoenix Endpoint URL if the endpoint is available and running.
Gets the configured repository module.
Gets the configured repository module, raising an error if not found.
Gets a configuration value as a string with type validation.
Gets configured prefix for urls or default value.
Gets the configured users module.
Checks if the configured mailer adapter is the local adapter.
Sets a configuration value.
Validates that required configuration is present.
Functions
Gets a specific configuration value.
Gets a specific configuration value with a default.
Examples
iex> PhoenixKit.Config.get(:mailer, PhoenixKit.Mailer)
MyApp.Mailer
iex> PhoenixKit.Config.get(:nonexistent, :default)
:default
@spec get_all() :: Keyword.t()
Gets all PhoenixKit configuration.
@spec get_base_url() :: String.t()
Gets configured host with an optional port or default value.
Gets a configuration value as a boolean with type validation.
Examples
iex> PhoenixKit.Config.get_boolean(:enabled, false)
true
iex> PhoenixKit.Config.get_boolean(:nonexistent, true)
true
@spec get_dynamic_base_url() :: String.t()
Gets the base URL dynamically from the parent Phoenix Endpoint if available, otherwise falls back to the static configuration.
This function automatically detects the correct URL from the running Phoenix application, which is especially useful in development mode where the port might be different from the default configuration.
Examples
iex> PhoenixKit.Config.get_dynamic_base_url()
"http://localhost:4001" # from Phoenix Endpoint
iex> PhoenixKit.Config.get_dynamic_base_url()
"http://localhost:4000" # fallback to static config
Gets a configuration value as a list with type validation.
Examples
iex> PhoenixKit.Config.get_list(:options, [])
[]
iex> PhoenixKit.Config.get_list(:nonexistent, [:default])
[:default]
@spec get_mailer() :: module()
Gets the configured mailer module.
Returns the configured mailer or falls back to PhoenixKit.Mailer.
Examples
iex> PhoenixKit.Config.get_mailer()
MyApp.Mailer
@spec get_parent_app() :: atom() | nil
Gets the parent application name that is using PhoenixKit.
This function attempts to detect the main application that has included PhoenixKit as a dependency.
Gets configuration from the parent application.
This is useful for accessing parent app mailer, endpoint, or other configurations that PhoenixKit needs to integrate with.
@spec get_parent_endpoint() :: {:ok, module()} | :error
Gets the parent application's Phoenix Endpoint module.
This function attempts to detect the main application's endpoint that is using PhoenixKit as a dependency.
Returns {:ok, endpoint_module} if found, :error otherwise.
@spec get_parent_endpoint_url() :: {:ok, String.t()} | :error
Gets the parent Phoenix Endpoint URL if the endpoint is available and running.
Returns {:ok, url} if successful, :error if the endpoint cannot be found
or accessed.
@spec get_repo() :: module() | nil
Gets the configured repository module.
@spec get_repo!() :: module()
Gets the configured repository module, raising an error if not found.
Examples
iex> PhoenixKit.Config.get_repo!()
MyApp.Repo
iex> PhoenixKit.Config.get_repo!()
** (ArgumentError) PhoenixKit repository not configured. Please set config :phoenix_kit, repo: YourApp.Repo
Gets a configuration value as a string with type validation.
Examples
iex> PhoenixKit.Config.get_string(:host, "localhost")
"example.com"
iex> PhoenixKit.Config.get_string(:nonexistent, "default")
"default"
@spec get_url_prefix() :: String.t()
Gets configured prefix for urls or default value.
@spec get_users_module() :: module()
Gets the configured users module.
@spec mailer_local?() :: boolean()
Checks if the configured mailer adapter is the local adapter.
Returns true if the mailer is configured to use Swoosh.Adapters.Local, which is typically used for development and testing environments where emails are stored locally rather than being sent to actual recipients.
Examples
iex> PhoenixKit.Config.mailer_local?
true # when using Swoosh.Adapters.Local
iex> PhoenixKit.Config.mailer_local?
false # when using a real mailer like SMTP or SendGrid
Sets a configuration value.
Examples
iex> PhoenixKit.Config.set(:repo, MyApp.Repo)
:ok
iex> PhoenixKit.Config.set(:custom_option, "custom_value")
:ok
Validates that required configuration is present.
Raises an exception if any required keys are missing.
Examples
PhoenixKit.Config.validate_required!([:repo, :secret_key_base])