grails: can one war file serve 2 different domains? - grails

In the Config.groovy file there is the setting:
grails.serverURL = "http://www.changeme.com"
Is there a way for a grails war file to be produced such that it can handle
"http://www.site1.com" and
"http://www.site2.com"
?

Per Ivar's comment response:
remove grails.serverURL and use container configuration
This did work. I happen to be running jetty 9. In case you are too, there is an example test.xml file in the webapps.demo directory. One can copy this in the webapps directory, rename it to the war name you are using, e.g. root.xml, and then just have a few entries in it:
set the name of your war file, e.g. root.war
fill in the virtualHosts area.

Related

DataSource.groovy classpath

def encryptedUid = getClass().getClassLoader()
.getResourceAsStream('user.txt')
.getText()
This code in the dataSource.groovy file works fine when I run it in a windows environment, but when I check the code in and Jenkins tries to load DataSource.groovy I get:
Error loading DataSource.groovy: Cannot invoke method getText() on null object.
The user.txt file is in the root of the src/java folder, and I know that it is built into the war file in a windows build. It just doesn't even get to building the war file on the Linux box.
Any ideas?
Apparently you're trying to configure the database username/password, but don't want to put them in DataSource.groovy directly for security reasons. Here's how I handle this:
Put the secret configuration in a file /grails-app/conf/secret.properties. The contents of this file are shown below:
dataSource.username=root
dataSource.password=secret
# other secret configuration
Include this file in the grails configuration by adding the following to Config.groovy
grails.config.locations = ["classpath:secret.properties"]
If you want to be able to override the config. in secret.properties on a per-environment basis, change this to
grails.config.locations = [
"classpath:secret.properties",
"classpath:secret-${Environment.current}.properties"
]
You can then (optionally) add a file secret-DEVELOPMENT.properties that will override the configuration in secret.properties in the dev environment, and likewise for the other environments.
Of course, in order for this to work, the secret*.properties files must be present when building the war (or executing run-app) and should not be checked into VCS.
You're not restricted to placing these config. files in a relative location to the classpath. You can put them anywhere on the file system by using the file: prefix instead of classpath:. Finally, you can put the secret configuration in a .groovy config. file instead of a .properties file.

How can I remove the application name from a Grails application’s URL?

I have an application running at a URL like this:
http://myapp.mydomain.com/myapp
I don’t want the /myapp part in the URL. So how can I get rid of the application name? I want just
http://myapp.mydomain.com
to be the URL. How can I do this?
bit detailed approach
First Method:
first shutdown your tomcat [from the bin directory (sh shutdown.sh)] then you
must delete all the content of your tomcat webapps folder (rm -fr *) then
rename your WAR file to ROOT.war finally start your tomcat
[from the bin directory (sh startup.sh)]
Second Method:
leave your war file in CATALINA_BASE/webapps, under its original name - turn off
autoDeploy and deployOnStartup in your Host element in the server.xml file.
explicitly define all application Contexts in server.xml, specifying both path
and docBase. You must do this, because you have disabled all the Tomcat
auto-deploy mechanisms, and Tomcat will not deploy your applications anymore
unless it finds their Context in the server.xml.
Note:
that this last method also implies that in order to make any change to any
application, you will have to stop and restart Tomcat.
Third Method:
Place your war file outside of CATALINA_BASE/webapps (it must be outside
to prevent double deployment). - Place a context file named ROOT.xml in
CATALINA_BASE/conf//. The single element in this context file MUST have a
docBase attribute pointing to the location of your war file. The path element
should not be set - it is derived from the name of the .xml file, in this
case ROOT.xml. See the Context Container above for details.
1) Your app server needs to be configured to have your grails application as ROOT application
2) your grails application context path should be "/" or app.context=/
You can make Tomcat serve a webapp as the root context by simply naming it ROOT.war, i.e. take the myapp-0.1.war generated by Grails and copy it to TOMCAT_DIR/webapps/ROOT.war.
If you have a setting for grails.serverURL in your Config.groovy you will need to override this for your production environment to ensure that any absolute links generated by Grails are correct
environments {
production {
grails.serverURL = 'http://myapp.mydomain.com'
}
}
But in Grails 2 it is usually safe to omit grails.serverURL entirely and let the app deduce the right value at runtime. You only need it if you're running behind a reverse proxy that doesn't pass through the correct Host header.

How does grails know where to look for property files and can I overried this location?

When I deploy my grails generated war file to jetty I get the following error. My question is how de we tell grails where to look for this config file? It works fine for grails run-app because its run from project base where this file exists but gives this error when running withing jetty.
org.codehaus.groovy.grails.commons.cfg.ConfigurationHelper - Unable to load specified config location file:./grails-app/conf/something-config.properties
If you're bundling the file in with the war, then putting it in grails-app/conf should put it in the root of the classpath. Move it to src/java if it doesn't, since that will definitely work both with run-app and in a war. Then register it as an external config in Config.groovy:
grails.config.locations = ["classpath:something-config.properties"]
If you want to deploy it separately from the war (for example to have one war that works in multiple deployments each with its own config file) then you would make the same change to Config.groovy, but copy it to somewhere in Jetty's classpath. I'm not that familiar with Jetty, but I know that Tomcat's lib dir is in its classpath, so I put files like this there. I assume there's an analagous location for Jetty where you can put jars and resources that should be loaded.
You can use externalized configurations
It's a good question. My Config.groovy has these lines commented out. I wonder if yours has a special external config you are trying to read:
// locations to search for config files that get merged into the main config
// config files can either be Java properties files or ConfigSlurper scripts
// grails.config.locations = [ "classpath:${appName}-config.properties",
// "classpath:${appName}-config.groovy",
// "file:${userHome}/.grails/${appName}-config.properties",
// "file:${userHome}/.grails/${appName}-config.groovy"]
// if(System.properties["${appName}.config.location"]) {
// grails.config.locations << "file:" + System.properties["${appName}.config.location"]
// }

Grails Config: include another config file

I have in my main config something like:
grails.config.locations = ["file:grails-app/config/Jawr.groovy"].
When running the application with grails run-app, everything is OK.
But, on deployment (creating the war archive) this does not work anymore, as the file "Jawr.groovy" is not kept anymore on the filesystem (it should be only in the war).
Do you have a solution for that? Hw do you include external files into the grails main configuration file?
Thanks.
Okay, a few things here.
First, because you don't have a leading slash on your config path, that is a path relative to who knows where. I played with this in Tomcat, and that path ends up being relative to the working directory you were in when starting the Tomcat server. If you start Tomcat, shut it down, change directories, then start it again, you are going to get two different config paths.
Second, the grails-app directory only exists within the source tree of your Grails project. The structure of an unpacked WAR file is more like the web-app folder of your Grails source tree, with folders like WEB-INF, META-INF, js, images, etc.
Third, you probably want to avoid putting your externalized config file inside the folder of your webapp. The next time you deploy your app, that configuration is going to get wiped away with the old version of the app. One of the points of the externalized config is so that you can redeploy without having to reconfigure.
A simple, but less than ideal, solution would be to use a static, fully qualified path, like /etc/yourApp/conf.groovy, then put that in the documentation. There is also a plug-in that handles this.
http://www.grails.org/plugin/external-config
I haven't used it, but the description makes it sound like it does sensible things.
see this: https://stackoverflow.com/questions/6341117/is-it-possible-that-grails-xxconfig-groovy-as-a-script-no-compile
Then I put it into /shared, and
modify:
//Config.groovy
grails.config.locations =
["file:shared/TZLibConfig.groovy"]
//BuildConfig.groovy
grails.war.resources = { stagingDir, args ->
copy(todir: "${stagingDir}/WEB-INF/shared"){
fileset(dir:"shared",includes:"**")
}
}
In my work, our team often use a system properties to save the path to the config file (often in home folder of the user running the app - for privilege sake). Then we manually copy the config file into that path
To identify that it's the production environment, we use the following code in Config.groovy:
if (System.properties["${appName}.config.location"]) {
grails.config.locations = ["file:" + System.properties["${appName}.config.location"]]
}
This article suggests allowing the user to specify the location of the config file as an environment variable or as a java property --- meaning you can simply specify it with -D on the command-line. This can be used in addition to all the other methods.

How can Grails load config external to WAR via HTTP?

I've seen questions and answers about how to specify external .groovy and .properties files for Grails config that are outside of the WAR file using grails.config.locations, but we need external config that isn't on the server on which the container (Tomcat) is running.
(It does have a local FS, but it is not persistient and is identical for all deployment environments - hence the need to override it external to the WAR, container and server).
So, can I use http: URLs for grails.config.locations ?
Yes. The following works:
Add this in Config.groovy and pass PARAM1 using -D to the JVM to specify the external URL for the config file.
grails.config.locations = ["url:" + System.properties["PARAM1"]]
I am not aware of any built in functionality that would allow you to specify URL for grails.config. However, it seems like something that you could implement yourself:
define your URL(s) that you want to use to d/l the Congif.groovy file that is included in your WAR
in that same Config.groovy script, write code to download the file to a specific location on your server's file system (see this article)
set your grails.config.locations to point to the file system location that you wrote the Config.groovy file to.
I haven't tried it but seems like it would work.

Resources