Hush
Hush makes it easy to configure your application at runtime and in release mode, it can retrieve configuration from multiple sources and is easily extensible.
You'd use Hush as a configuration tuple, which gets replaced at runtime, this is useful to inject configuration that is not known at compile time.
# config/prod.exs
alias Hush.Provider.{GcpSecretManager,SystemEnvironment}
config :your_app_name, Web.Endpoint,
http: [port: {:hush, SystemEnvironment, "PORT", [cast: :integer]}]
secret_key_base: {:hush, GcpSecretManager, "secret_key_base"}
Hush ships with a SystemEnvironment
provider which reads environmental variables, but multiple providers exist to make your life easy in reading from other sources:
Provider | Description |
---|---|
SystemEnvironment | Read environmental variables |
GcpSecretManager | Load secrets from Google Cloud Platform's Secret Manager. |
Installation
Add hush
to your list of dependencies in mix.exs
:
def deps do
[
{:hush, "~> 0.0.1"}
]
end
Run mix deps.get
to install it.
Configuration
Some providers may need to initialize applications to function correctly. SystemEnvironment
does not require any initialization and does not need to be in the list below.
# config/config.exs
config :hush,
providers: [
GcpSecretsManager
]
Resolving Runtime Configuration
Hush can be loaded by calling it directly, or by using its release Config.Provider.
Loading via direct call
# application.ex
def start(_type, _args) do
Hush.resolve!()
end
Loading via Config Provider
# mix.exs
def project do
[
# ...
releases: [
your_app_name: [
config_providers: [{Hush.ConfigProvider, nil}]
]
]
]
end
If you are using Hush in both release and non-release mode, you still want to load it directly:
# application.ex
def start(_, _) do
unless Hush.release_mode?(), do: Hush.resolve!()
end
Usage
The configuration tuple is defined by a :hush
atom, a provider module, a key for the provider and an optional list of options.
{
:hush,
provider :: module(),
key :: String.t(),
options :: [
default: any(),
cast: :string | :integer | :float | :charlist | :atom
]
}
Defaults
By default if a given key
is not found by the provider, Hush will raise an error. To prevent this, provide a default
in the options
component of the tuple:
# config/prod.exs
alias Hush.Provider.SystemEnvironment
config :your_app_name, Web.Endpoint,
url: [host: {:hush, SystemEnvironment, "HOST", [default: "my-app.example"]}]
Casting
# config/prod.exs
alias Hush.Provider.SystemEnvironment
config :your_app_name, Web.Endpoint,
http: [port: {:hush, SystemEnvironment, "PORT", [cast: :integer, default: 4000]}]
License
Hush is released under the Apache License 2.0 - see the LICENSE file.