ENV Files
Background
One of the potential drawbacks for using JSON configuration is that those files eventually need to be shared and potentially checked into version control. Ideally, it would be possible for the .cfConfig.json to be checked into a repo so everyone would know what settings needed to be set but the actual passwords (keys, tokens, private stuff) would be pulled from someplace locally. That is the problem that .env files solve.
Env files do NOT get checked into repos because they are designed to contain secret information which should not be broadcast.
Hiding our secrets
In CommandBox ,install the commandBox DotEnv module by typing
install commandbox-dotenv.In the root of your site, create a file called .env.
You can either edit the file in your IDE or use
dotenv set name value. We're going to set several values in our .env.dotenv set DB_USER usernamedotenv set DB_NAME datbasenamedotenv set DB_PASSWORD passworddotenv set DB_DATABASE dbNamedotenv set DB_CLASS com.mysql.cj.jdbc.Driverdotenv set DB_SCHEMA schemaNamedotenv set DB_BUNDLE com.mysql.cjdotenv set DB_BUNDLEVERSION 8.0.15dotenv set DB_TYPE mysqldotenv set DB_HOST justenough.cyqisqc1fx3i.us-west-2.rds.amazonaws.comIn the .cfConfig.json file, replace the database username with
${DB_USER}the database password with${DB_PASSWORD}the database name with{$DB_DATABASE}the class with${DB_CLASS}the database name with${DB_NAME}the host with${DB_HOST}Restart the server with
server restartOpen the site in the browser and see what happens.
You might ask, "But how will other people on the team know what settings need to go into the .env file?". This is an excellent quesion and we'll address it in the next session.
Last updated