CFConfig / local vs production

Hi, I’ve been using .cfconfig.json in the root to setup data sources, etc. It’s been working quite well.

I’m now hoping to load a different version of cfconfig when running on my local machine (mainly just a different mysql user to connect to the db). Any advice on how to best do this?

Thanks!
David

Hi @DavidRepas ! There are many ways to do this, but I suppose the “best” one depends on your setup. If all you need to do is swap out the MySQL user, I’d recommend simply using an environment variable for this.

So you just use the ${MY_ENV_VAR} placeholder in your JSON file and then you can create a .env file locally on your machine that looks like this

MY_ENV_VAR=myLocalPassword

Ensure you have the commandbox-dotenv module installed (comes out of the box now in CommandBox 5.8.0) and it will load up that env var as your server starts. On production, simply specify a different environment variable, which can be a “proper” server env var, or a docker secret, etc, etc. Just ensure if you use a .env in production that your web server is blocking access to it.

You can also cheat and only specify the env var in one location and make the other the default value. So, for example, if your JSON file had this:

"password": "${DB_PASSWORD:defaultPassword}"

Then the contents of the DB_PASSWORD env var would be used if it existed, and when it didn’t the default value would be put in.

And another option that doesn’t require you to touch your JSON file at all would simply be to provide this env var on dev

cfconfig_datasources_yourDSName_password=yourPassword

and that will automatically swap out the password for the yourDSName datasource regardless of what was in the JSON.

And there are many more options that don’t involve env vars. You could swap out the entire CFConfig.json file for dev/prod, but that feels like overkill just to change a password.

Many options, your choice :slight_smile:

Hi @bdw429s that’s pretty awesome. If I did want to swap the entire file, would this work?

On my local Windows machine, set an environment variable to a different cfconfig file, but leave the .cfconfig.json file in the root as well. Would that load the environment variable version that I point to when on my local machine, but still use the root version when loading somewhere else?

Thanks,
David

If the file is named .cfconfig.json, it will be loaded automatically when the CommandBox server starts. There are several conventions for specifying other JSON files either in your server.json or by env var.

Take your pick. But really, most solutions boil down to using an env var, so if you just want to override a few specific bits, I’d just override those directly with env vars instead of swapping out the entire file to keep from managing to separate files.