Grails: Inject application configuration from application.properties into Config.groovy - grails

Here is the problem:
For the sake of multiple developers and development environments (developer dependent, developer VM, dev, qa, stage, prod), I want to be able to use application.properties file to inject app confiugration into Config.groovy. Reason why it works for us is that we are using same file for injecting settings in Spring's applicationContext.xml file. I've been trying to accomplish this by follwing in Config.groovy, but still not geting value for neo4j.url from application properties.
grails.config.locations = [
"classpath:application.properties",
"file:${baseDir}/application.properties",
"file:${basedir}/application.properties"
]
println "neo4j url : ${grailsApplication.config.neo4j.url}"
println "neo4j url : ${neo4j.url}"
There must be something I'm doing wrong..

Problem was that I was printing values in Config.groovy, once application loaded everything worked fine, as expected, case closed tnx to Igor.

Related

Log4j is already not configured via logback.groovy file in Grails 3.2.8

As we know by default logging in Grails 3.0 is handled by the Logback logging framework and can be configured with the grails-app/conf/logback.groovy file. The good part of this was that the configuration inside this file was applicable for the Logback and Log4j implementations (tested successfully in Grails 3.1.2). After we migrated to use Grails 3.2.8 from Grails 3.1.2 (as I said before), it seems that the configuration inside the logback.groovy file is not used by the Log4j and it should be configured in other way - at runtime.
I made a deep research in this to ensure that this is really the way it is configured now and it seems that if I am using the default Log4j logging:
org.apache.log4j.Logger.getLogger("name").info("TEST");
is not working at all. To make the information populated into the output log file, I should use the default SLF4J API:
org.slf4j.LoggerFactory.getLogger("name").info("TEST");
The problem here is because that we have a lot of places in the code where the logging is implemented using Log4j approach and now - nothing is written in the log file.
So I have two questions here. The first one - is this the exact way how Grails implemented this behavior (as nothing is mentioned in the official documentation - Grails 3.2.8 Documentation). The second one - is the XML or RUNTIME configurations are the best way to configure the Log4j, so use it as before. If yes - the bad thing here is that we should maintain two same configurations at a time. Is there any other way to configure the Log4j to use the same configuration as the Grails Logger.
P.S. There is no need to publish any code, as all are just default configurations.
Thanks a lot!
appender('STDOUT', ConsoleAppender) {
encoder(PatternLayoutEncoder) {
charset = Charset.forName('UTF-8')
}
}
root(ERROR, ['STDOUT'])
logger("<package_name>", DEBUG)
List artifacts = ["conf", "controllers", "domain", "init"]
artifacts.each { artifact ->
logger("grails.app.${artifact}.<package_name>", DEBUG)
}
After writing this, you can add logger for debugging in any artifacts mentioned above like:
log.debug "Any Message"
For more setting you can read here: https://logback.qos.ch/manual/groovy.html

Not able to access web-app folder files in Grails 3

I am not able to access web-app folder files in Grails 3.
I have robots.txt in the web-app folder and in Grails 2 I was able to access it directly at http://localhost:8080/robots.txt. After migrating to Grails 3 I am not able to access it anymore at http://localhost:8080/robots.txt.
How can I make these files accessible again?
See https://github.com/grails/grails-core/releases/tag/v3.0.12 and part Location of static resources
In Grails 3 you can store your files under src/main/resources. You can access them by filename preceded with static for example http://localhost:8080/static/robots.txt.
This path can be changed using config option as defined in attached URL
I faced a similar problem in Grails 3.3.1. I had the necessity to access a file template to use it with a plugin (excel-export plugin). After reading the documentation, I put the file under src/main/resources. It worked with OK in development mode (grails run-app), but I received an error in production environment (grails war). After a lot of reading, I've found the way to make it work. I've let the file in the same directory (src/main/resources), and then, in my controller:
def template = this.class.classLoader.getResource('myExcelFile.xlsx')
def path = template.file //will give you the real path to your file
Having the path, you can then open a stream or do what you need to do. In my case, use it whith the plugin:
new WebXlsxExporter(path).with {
setResponseHeaders(response)
add(products, withProperties)
save(response.outputStream)
}

Spring Cloud Config Server - Where to set encrypt.key to enable /encrypt endpoint

I have a Spring Cloud Config app with the Spring Cloud Security dependencies. I'm trying to hit the /encrypt endpoint to encrypt a password.
According to the docs at http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_key_management I believe I need to set a symmetric key using "encrypt.key". But I can't figure out where to put this; all combinations I've tried result in {"description":"No key was installed for encryption service","status":"NO_KEY"} when I attempt to POST to /encrypt.
If I POST a key to /key, the /encrypt works perfectly, so I'm pretty sure that everything else is setup correctly. It also works fine using the environment variable ENCRYPT_KEY, or by using a system property encrypt.key. I just can't figure out where to place the encrypt.key within a configuration file. Is there a sample on this?
Thanks #pVilaca, this is indeed what the documentation says, but setting the key in application.properties or application.yml won't work.
Through experimentation I've found that the only place you can set the encrypt.key property is an ENCRYPT_KEY environment variable, a system property, bootstrap.properties, bootstrap.yml, or calling the /key endpoint.
Not sure why application.properties or application.yml don't work in this case. Setting this key must have more of an impact on the startup process than it would appear.
Setting Spring Cloud Version to 'Brixton.SR5' worked for me. For some reason, setting 'encrypt.key' in either application.properties/yml or bootstrap.properties/yml in later Spring Cloud Version does not work anymore.
check properties tag of pom.xml file. If you are using version 2 or 3 of Dalston, change it to:
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
and it should be working fine. In my case i was using version 3 and changing it to version 1 resolved the issue for me.
Reference:
https://github.com/spring-cloud/spring-cloud-config/issues/767
If you're using spring cloud with spring boot, that is the method that is described on the documentation that you mentioned, you've two 'main' properties files.
bootstrap.[properties,yml]
To modify the startup behaviour you can change the location of the config server using bootstrap.properties (like application.properties but for the bootstrap phase of an application context)
application.[properties,yml]
where the "application" is injected as the "spring.config.name" in the SpringApplication (i.e. what is normally "application" in a regular Spring Boot app)
source: Spring Cloud Config Documentation
So, it should be enough to set the encrypt.keyin your application.[properties,yml] file (or the alternative name if specified)

Griffon db4o plugin configuration

I'd like to use db4o inside Griffon app, but I've problem with configuration:
how to set up plugin to not delete dbFile during AppShutdown.
There is some info about configuration at plugin page:
def configure(EmbeddedConfiguration configuration) {dataSource.delete=???}
but hot exactly set datasource.delete = 'false'?
Thanks for your help.
panKonrad.
Look closely at the different environments defined in Db4oConfig.groovy; you'll notice that the production environment does not delete the database file. That's exactly how you would need to set it up in your custom environment. By default both the development and test environment will delete the file.

How to get base URL in Websphere application Server?

I am running my application in WAS6.1. In the same server I have two EAR deployed. One application can be accessed using the URL server.com:port/app1 over http and the other one as server.com:port/app2. In the application1 I am referencing application2 as :
<media-access-proxy
base-url="http://ipaddress:port/app2"
prefetch-base-url=""
mode="mode1"/>
Since this ipaddress is static so everytime I have to change this if I am moving from dev environment to QA and from QA to production. I want to set it such as that is should take the base url by itself. Does WAS has a properties file like was.baseurl which could be placed in place of ipaddress:port? Or something like that?
The schema requires URI so you can't use anything special. However you can use a DNS name which is same for both environments. Use something like http://server:9080/app2 and add server to both servers' hosts file.

Resources