Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails: can settings in Config.groovy be overridden programmatically at runtime?

Tags:

grails

The mail plugin is documented to require its settings in Config.groovy. I want to have it stored in the database so it can be changed at runtime. And this is just one example.

I've read Does externalizing sensitive config variables into a .properties outside of Config.groovy provide a security advantage in Grails? but this is about using an external property file, I need to store the settings in the database (the app has an admin interface, with auditing and so on).

Thank you.

like image 380
wishihadabettername Avatar asked Jan 12 '23 06:01

wishihadabettername


2 Answers

In Bootstrap.groovy you can read the property values from the database and replace those read from the config files by updating grailsApplication.config. For example, assume you want to replace the value of a config property named foo.bar

class BootStrap {

    GrailsApplication grailsApplication

    def init = { servletContext ->

        def fooBarDB = // read the value of foo.bar from the database
        grailsApplication.config.foo.bar = fooBarDB
    }
}

Rather than writing this code yourself, you could instead use the Dynamic Config Plugin to achieve the same outcome.

like image 167
Dónal Avatar answered May 26 '23 18:05

Dónal


Yes, you can do this. During the grails bootstrap process using Bootstrap.groovy, retrieve the properties from the database and override them. It's similar to a properties file override, you're just storing them in a database.

Also see Properties in the database

like image 43
Jeff Storey Avatar answered May 26 '23 19:05

Jeff Storey