I was frustrated with how I was using configuration variables especially when it came to variables that were embedded into a dependency app.
My problem
I don’t like having to search around for where a particular configuration value lives within the code across dependencies.
I don’t like having to remember the dependency app structure to override particular values in a parent app.
I don’t like how much inconsistency has come from my previous methods.
My solution
I create ENV dependency app that handles environment variables. It allows dependency apps to register environment variables but provide a default value even if none of the setup is complete. Giving dependencies a “batteries included” feel to them. If all of the default values work, great! There is no additional work on the parent app. If you want to change a config value in the parent app there is some additional setup and ENV
provides tools to review the available configuration values.
Features
Common Interface
Variables can be retrieved by environ.get(VARIABE_NAME)
Varaibles throughout the app, dependencies included, can be set from a top level table called env
.
Default values
Providing default values can make dependencies work right out of the box without any additional work from the parent app.
from ENV import environ
my_variable = environ.get('MY_VARIABLE', 1234)
Forced values
You can force values to be included in the environment variables.
from ENV import environ
api_public_key = environ.get('API_PUBLIC_KEY')
Which will raise a LookupError
if it is not available in the environment variables. This is nice when you have some configuration values that will be specific to the parent app and should never be reused between apps.
Variable Tracking
ENV automatically collects environment variables throughout the app and provides access to them from the server console.
This allows me to place my variable definition closer to the point of use which is better for readability, debugging and adjusting default values.
This can be retrieved from the server console:
>> from ENV import environ
>> environ.VARIABLES
Environment Variables
in_use:
API_PUBLIC_KEY
available:
MY_VARIABLE
API_ENDPOINT
API_PUBLIC_KEY_ROTATION_SCHEDULE
available
are variables that are currently using the default value and can be overridden.
in_use
are variables that are currently being pulled from the environment variables table.
Setting variable
It is possible to set variables programatically as well. This can only be done if the env
table is setup.
from ENV import environ
environ.set(variable_name, variable_value, variable_info)
Clone
Or use as third party dependency with token:
AFKPYPDLJMH2TYVK