Grails - How can I get serverURL in config.groovy? - grails

I want to use ServerURL in config.groovy.
But, I need configure url.setting both development and production if I cannot use serverURL in config.groovy.
I try to get serverURL like below, it makes error.
config.groovy
def grailsApplication
environments {
development {
grails.logging.jul.usebridge = true
grails.serverURL = "http://localhost:8080"
}
production {
grails.logging.jul.usebridge = false
grails.serverURL = "http://myapp.com"
}
}
url.setting = "${grailsApplication.config.grails.serverURL}"
the error when I run app is like below
Error Error packaging application: Error loading Config.groovy: Cannot get property 'config' on null object (Use --stacktrace to see the full trace)

You can get the server URL like-
url.setting="${grails.serverURL}"

Related

Grails oauth plugin external configuration in production mode

I'm using grails oauth plugin standard configuration as part of Config.groovy
environments {
production {
oauth {
providers {
linkedin {
api = LinkedInApi
key = 'mykey'
secret = 'mySecret'
successUri = "/userLogin/linkedinLogin"
callback = "https://myserverdomain/oauth/linkedin/callback"
}
}
}
}
}
When using the same configuration in external file and reference it by
grails.config.locations = ["file:external-config.groovy"]
There is an error "No oauth configuration found. Please configure the oauth scribe plugin".
Notice:
1. other configuration properties are working fine.
2. Problem exist only in production mode. Using the same configuration file in development mode works fine.

externalizing configurations for test.datasource

I have been able externalize DataSource configurations for development environment by updating grails.config.locations in Config.groovy and then setting specific configurations in .grails/${appName}-config.properties as following:
dataSource.driverClassName = com.mysql.jdbc.Driver
dataSource.url = jdbc:mysql://localhost/db-dev
dataSource.username = root
dataSource.password = pass
For the 'test' environment, i have different Database so i would like to updated test.dataSource.url accordingly but this doesn't work. It still takes the 'dev' dataSource.url when running 'grails test' mode(i.e. grails test dgm-update). How to externalize test environment DataSource configuration?
Thank You for helping
grails.config.locations = ["file:${userHome}/.grails/${appName}-config-${grails.util.Environment.current.name}.properties"]
and then put nested files for each env:
/home/me/.grails/myapp-config-dev.properties
/home/me/.grails/myapp-config-test.properties
/home/me/.grails/myapp-config-production.properties
Try to use Groovy external configuration, i.e. ".grails/${appName}-config.groovy" with environment specific DSL:
environments {
test {
dataSource {
...
}
}
}

Error running grails run-war after half a day

I have a 2.1.1 grails app which I am running using grails run-war. It works well, and runs for quite some time, but stops after something between 12 and 24 hours. I am never observing the app when it fails. It always just says "bad"
$ grails run-war
| Done creating WAR target/web-0.1.war
| Running Grails application
Tomcat Server running WAR (output written to: target/tomcat-out.txt)
| Server running. Browse to http://localhost:8090/web
bad
$
2 of the last 3 failures has produced this in the output file:
2012-10-10 10:22:46,684 [localhost-startStop-2] ERROR loader.WebappClassLoader - The web application [/web] appears to have started a thread named [Poller SunPKCS11-Darwin] but has failed to stop it. This is very likely to create a memory leak.
2012-10-10 10:22:46,694 [localhost-startStop-2] ERROR loader.WebappClassLoader - The web application [/web] appears to have started a thread named [MongoCleaner1292971653] but has failed to stop it. This is very likely to create a memory leak.
However most of the time, there is nothing in the log file.
This happens even if nobody has used the webapp, ie its been idle. The plugins in use are:
runtime ":mongodb:1.0.0.GA"
runtime ":jquery:1.7.2"
compile ":jquery-ui:1.8.15"
runtime ":resources:1.2-RC1"
// Uncomment these (or add new ones) to enable additional resources capabilities
//runtime ":zipped-resources:1.0"
//runtime ":cached-resources:1.0"
//runtime ":yui-minify-resources:0.1.4"
build ":tomcat:$grailsVersion"
WHen using grails 2.0.4, this same app ran for weeks without failing. The only change is the upgrade to grails 2.1.1
Any thoughts?
Thanks
Edit:
Including the DataSource.groovy
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb;MVCC=TRUE"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb;MVCC=TRUE"
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}
Then in each Model,
class Album {
static mapWith = "mongo"
I do this to ensure I use Mongo as each time I upgrade grails, it automatically re-installs the hibernate plugin even though I keep uninstalling it.
Thanks

Grails: Is there a debug flag I can check?

I have a few log.debugs() that I don't want to process (since they are heavy) unless the app is currently in debug mode (not production).
Is there a way to check if the grails app is currently in debug mode/development mode?
You can test if the current environment is dev (for example) using the following:
import grails.util.Environment
if (Environment.current == Environment.DEVELOPMENT ) {
// Do your dev logging here
}
IMO, a better solution than hard-coding the env where this logging happens, is to configure it. For example, to enable debug logging for this class only in the dev environment. add the following to Config.groovy
log4j = {
appenders {
// config for stdout and logfile appenders omitted
}
// log everything at error level to stdout and logfile appenders
root {
error 'stdout', 'logfile'
}
environments {
development {
// log this class at debug level in dev env only
debug 'com.example.MyClass'
}
}
}
in the config.groovy the environments are defined.
you can specify what you want the log configuration to be based on the environment the application in running in
environments {
development {
log4j = {
// determine what appenders are logging in development...
}
}
production {
log4j = {
// determine what appenders are logging in production...
}
}
}

How to extend per environment configuration in grails

It seems that only grails.serverURL and grails.path are recognized as per environment configrautions. bla and foo are ignored and could not be used in application
Anyone could solves this and provide a way to get bla and foo configured per environment?
environments {
production {
grails.serverURL = "http://alpha.foo.de"
grails.path = ""
bla = "text"
foo= "word"
}
test {
grails.serverURL = "http://test.foo.de"
grails.path = ""
bla = "othertext"
foo= "otherword"
}
}
Since Grails 3.0, both Groovy or YAML syntax can be used. The new main application configuration file is /conf/application.yml but you can continue to use your existing groovy configuration defining a /conf/application.groovy file.
This is an example of datasource definition per environment (in YAML):
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:devDb
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb
production:
dataSource:
dbCreate: update
url: jdbc:h2:prodDb
myenv:
dataSource:
dbCreate: update
url: jdbc:h2:myenvDb
To run a grails command in a specific environment you can use:
grails [environment] [command name]
To target an environment different from dev, test and prod you can use:
grails -Dgrails.env=myenv run-app
See the grails environment documentation or this example application for more information.
All the ConfigSlurper variables are scoped by environment. What you've shown above should work fine.
When you use grails run-app, you are running in the development environment by default. Could that be your issue?
Try...
> grails prod run-app
This should give you bla (text) and foo (word)
> grails test run-app
This should give you bla (othertext) and foo (otherword)
Config.groovy setting
environments {
development {
grails.serverURL = "http://alpha.foo.de"
grails.path = "/bar"
staticServerURL = "http://static.foo.de"
staticPath = "/static"
}
}
source code index.gsp
${grailsApplication.config.staticServerURL}a
${grailsApplication.config.staticPath}b
${grailsApplication.config.grails.serverURL}c
${grailsApplication.config.grails.path}d
what is printed out when started with grails run-app
a b http://alpha.foo.dec /bard
Check in your Config.groovy that you have the import for Environment
import grails.util.Environment
Otherwise the Environment.current is empty.

Resources