JSON Configuration Includes
By Mark Fletcher
- 2 minutes read - 298 wordsEverything needs configuration data. Some people use .toml files, others use .ini or .yaml. I don’t like any of those file formats and I also just prefer JSON for data in general. So at groups.io all our configuration data is in JSON formatted files. But I wanted the ability for config files to include other config files, which is not something that JSON normally supports, so I wrote a small utility function to support including JSON files in other JSON files. This function is used by every program that runs the groups.io backend.
What this function does is take the name of a JSON file as well as a struct to read it into. In the struct, it looks for a string slice called Includes. That slice contains the names of other JSON files to read in. It does this recursively. I’ll illustrate with an example.
We have a web server that needs to talk to the user database. Here is the configuration data needed for the web server.
And here is the user database configuration data.
Here is a config file for the user database.
And here is the config file for the webserver.
You see that the webserver config file has an Includes slice that references the userdb config file. Calling ReadConfiguration() will populate the webserver config struct with the userdb config information.
Groups.io is the best tool to get a bunch of people organized and sharing knowledge. Start a free trial group today.