Dave Shuck
Dave Shuck

Reputation: 563

How do I derive physical path of a relative directory inside Config.groovy?

I am trying to set up Weceem using the source from GitHub. It requires a physical path definition for the uploads directory, and for a directory for appears to be used for writing searchable indexes. The default setting for uploads is:

weceem.upload.dir = 'file:/var/www/weceem.org/uploads/'

I would like to define those using relative paths like WEB-INF/resources/uploads. I tried a methodology I have used previously for accessing directories with relative path like this:

  File uploadDirectory = ApplicationHolder.application.parentContext.getResource("WEB-INF/resources/uploads").file
  def absoluteUploadDirectory = uploadDirectory.absolutePath
  weceem.upload.dir = 'file:'+absoluteUploadDirectory

However, 'parentContext' under ApplicationHolder.application is NULL. Can anyone offer a solution to this that would allow me to use relative paths?

Upvotes: 0

Views: 4510

Answers (3)

moskiteau
moskiteau

Reputation: 1102

i.e.

            String base = System.properties['base.dir']     
            println "config: ${base}/web-app/config/HookConfig.grooy"
            String str = new File("${base}/web-app/config/HookConfig.groovy").text
            return new ConfigSlurper().parse(str)

or

def grailsApplication    
private getConfig() {
            String str = grailsApplication.parentContext.getResource("config/HookConfig.groovy").file.text
            return new ConfigSlurper().parse(str)
}

Upvotes: 0

OverZealous
OverZealous

Reputation: 39570

To add to Aram Arabyan's response, which is correct, but lacks an explanation:

Grails apps don't have a "local" directory, like a PHP app would have. They should be (for production) deployed in a servlet container. The location of that content is should not be considered writable, as it can get wiped out on the next deployment.

In short: think of your deployed application as a compiled binary.

Instead, choose a specific location somewhere on your server for the uploads to live, preferably outside the web server's path, so they can't be accessed directly. That's why Weceem defaults to a custom folder under /var/www/weceem.org/.

If you configure a path using the externalized configuration technique, you can then have a path specific to the server, and include a different path on your development machine.

In both cases, however, you should use absolute paths, or at least paths relative to known directories.

Upvotes: 2

Aram Arabyan
Aram Arabyan

Reputation: 2359

look at your Config.groovy you should have (maybe it is commented)

// locations to search for config files that get merged into the main config
// config files can either be Java properties files or ConfigSlurper scripts

// "classpath:${appName}-config.properties", "classpath:${appName}-config.groovy",
grails.config.locations = [
        "file:${userHome}/.grails/${appName}-config.properties",
        "file:${userHome}/.grails/${appName}-config.groovy"
]

Create Conig file in deployment server

"${userHome}/.grails/${appName}-config.properties"

And define your prop (even not relative path) in that config file.

Upvotes: 3

Related Questions