Created
May 16, 2012 10:17
-
-
Save pledbrook/2709274 to your computer and use it in GitHub Desktop.
Loading runtime config from JSON in Grails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... 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) : [:] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With this code, you can add an environment variable containing JSON data that overrides the settings in Config.groovy:
This is useful for environments (such as clouds) in which you can't add a config file to the local file system.