【问题标题】:Grails access current environment from within config.groovyGrails 从 config.groovy 中访问当前环境
【发布时间】:2013-08-15 18:27:54
【问题描述】:

使用 Grails OAuth 插件需要在 Config.groovy 中提供绝对回调 URL。但是,我对每个环境都有不同的 serverURL。

有没有办法从 Config.groovy 中获取当前环境,这是我想要做的一个例子:

def devServerUrl = 'http://dev.example.com'
def prodServerUrl = 'http://prod.example.com'
def currentServerUrl = grailsApplication.metadata.environment == 'development' ? devServerUrl : prodServerUrl;

environments {
    development {
        grails {
            serverURL = devServerUrl
        }
    }
    production {
        grails {
            serverURL = prodServerUrl
        }
    }
}

oauth {
    providers {
        runkeeper {
            api = RunKeeperApi
            key = 'key'
            secret = 'secret'
            callback = currentServerUrl + '/oauth/runkeeper/callback'
        }
    }
}

有什么想法吗?谢谢!

【问题讨论】:

  • 我认为 Environment.current 在 Config.groovy 中工作

标签: grails configuration


【解决方案1】:

试试这个:

def currentServerUrl = Environment.current.name == 'development' ? devServerUrl : prodServerUrl;

【讨论】:

  • 工作得很好,谢谢!我还必须在 Config.groovy 的顶部导入 grails.util.Environment。
  • 或者使用内置常量:Environment.current == Environment.DEVELOPMENT
【解决方案2】:

我认为为每个环境设置不同的grails.serverURL 然后做:

callback = "${grails.serverURL}/oauth/runkeeper/callback"

【讨论】:

    猜你喜欢
    • 2015-04-01
    • 1970-01-01
    • 2012-10-20
    • 2018-04-18
    • 2018-10-02
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    相关资源
    最近更新 更多