Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pledbrook
Created May 16, 2012 10:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pledbrook/2709274 to your computer and use it in GitHub Desktop.
Save pledbrook/2709274 to your computer and use it in GitHub Desktop.
Loading runtime config from JSON in Grails
// ... rest of Config.groovy content goes above this line to ensure that
// the JSON overrides existing settings.
ConfigLoader.addEntries(loadJson(fetchJson()), this)
def fetchJson() { return System.getenv("GRAILS_APP_CONFIG") }
def loadJson(content) { return content ? grails.converters.JSON.parse(content) : [:] }
// e.g. in grails-app/utils/ConfigLoader.groovy
class ConfigLoader {
static void addEntries(Map data, obj = null) {
data?.each { key, value ->
if (value instanceof Map) {
addEntries(value, obj.getProperty(key))
}
else obj.setProperty(key, value)
}
}
}
@pledbrook
Copy link
Author

With this code, you can add an environment variable containing JSON data that overrides the settings in Config.groovy:

export GRAILS_APP_CONFIG='{"dummy": {"test": 1, "str": "Stuff", "l": [2, 3, 6]}}'

This is useful for environments (such as clouds) in which you can't add a config file to the local file system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment