grails 2.0 - correct use of serverURL for production?

Ray picture Ray · Feb 8, 2012 · Viewed 15.2k times · Source

Grails 2.0 changed with the way it uses grails.serverURL for development and test environments (as described in the manual). However, I've had several problems with serverURL in regard to production deployment on Tomcat. There seem to be several options for serverURL (production mode):

  • (Added) this setting is just "smoke and mirrors", because Tomcat and Jetty have methods to control how the URL is handled for the App, down to the "App" level.

  • Use it to specify the server (as is pointed out as a "TODO" in Config.groovy)

  • Don't use it as indicated here by one of the Grails contributors, i.e. "It should always be safe to remove the serverURL property and let Grails generate urls relative to the current running app." It's not clear if this extends to production or not (when not generating emails).

  • Use another method instead, namely grails.app.context, which is not documented in Grails 2.0 manual, but is described in certain references, here and here.

Can you please clarify the correct use of serverURL and app.context, both for Jetty and Tomcat (production mode)?

Thanks

Answer

miek picture miek · Dec 20, 2012

Good question! I was just looking for the correct way to get the actual serverURL without explicitly needing to configure it in Config.groovy (in a Grails 2.1 application).

Since Grails 2.0, there is a super-useful class called LinkGenerator that you can use virtually anywhere, for example in a Service:

import org.codehaus.groovy.grails.web.mapping.LinkGenerator

Class MyService {
    LinkGenerator grailsLinkGenerator

    String serverUrl() {
        // Generate: http://localhost:8080/link-generator
        grailsLinkGenerator.serverBaseURL
    }
}

Thanks to Mr. Haki for blogging about this!