Prius: environmentally-friendly app config
By Harry MarrMay 20151 min read
We just open-sourced Prius, a simple library for handling environment variables in Ruby.
Safer environment variables
Environment variables are a convenient way of managing application config, but it's easy to misconfigure or forget them. This can cause big problems:
# If ENCRYPTION_KEY is missing, a nil encryption key will be used encrypted_data = Crypto.encrypt(secret_data, ENV["ENCRYPTION_KEY"]) # If FOO_API_KEY is missing, this code will bomb out at run time FooApi::Client.new(ENV.fetch("FOO_API_KEY")).make_request
Prius helps you guarantee that your environment variables are:
- Present - an exception is raised if an environment variable is missing, so you can hear about it as soon as your app boots.
- Valid - an environment variable can be coerced to a desired type (integer, boolean or string), and an exception will be raised if the value doesn't match the type.
# Load a required environment variable (GITHUB_TOKEN) into Prius. Prius.load(:github_token) # Use the environment variable. Prius.get(:github_token) # Load an optional environment variable: Prius.load(:might_be_here_or_not, required: false) # Load and alias an environment variable: Prius.load(:short_name, env_var: "LONG_NAME_WE_HAVE_NO_CONTROL_OVER") # Load and coerce an environment variable (or raise): Prius.load(:my_flag, type: :bool)
How we use Prius
All environment variables we use are
loaded as our app starts, so we catch
config issues at boot time. If an app can't boot, it won't be deployed, meaning
we can't release mis-configured apps to production.
We check a file of dummy config values into source control, which makes running the app in development and test environments easier. Dotenv is used to automatically load this in non-production environments.