在Revel的app.conf文件中读取环境变量。

huangapple go评论82阅读模式
英文:

Read environment variables in revel app.conf

问题

Revel使用app.conf来存储配置信息。我想让我的配置从环境变量中获取值,使用os.Getenv(key)方法。

我应该如何做呢?我应该使用revel.Config来进行更改吗?如果是这样,我应该把它放在哪里?

还是有其他的方法吗?

我主要是用它来存储数据库信息(我不想在配置文件中输入我的凭据)。

谢谢。

英文:

Revel uses app.conf for storing configuration. I want my configuration to get value from environment variable using os.Getenv(key)

How can I do it? Should I use revel.Config to make changes? if so where do I place it?

Or is there another way?

I use it mainly for storing database information (I don't want to enter my credential in configuration files)

Thank you

答案1

得分: 3

Revel 使用 revel/config 来管理 app.conf

而 revel/config 仅在 type.go (c *Config) String() 方法中从环境变量中读取。

// $ 环境变量
computedVal, _ = c.computeVar(&value, envVarRegExp, 2, 1, func(varName *string) string {
return os.Getenv(*varName)
})

这意味着你可以在配置文件中根据环境变量的名称添加值,从而可以使用该环境变量来修改配置。

revel/config REAMD.md 文件 中有一个示例。

[DEFAULT]
host: www.example.com
protocol: http://
base-url: %(protocol)s%(host)s    <====

OP pveyes 指出type.go 中的这个注释

// substitute by new value and take off leading '%(' and trailing ')s'
// %(foo)s => headsz=2, tailsz=2
// ${foo} => headsz=2, tailsz=1

所以:

> - 使用环境变量时使用 ${ENV_VARS},和

  • 用于展开变量时使用 %(UNF_VARS)s

英文:

Revel uses revel/config for managing app.conf.

And the only place revel/config foes read from the environment variable is in type.go (c *Config) String() method.

// $ environment variables
computedVal, _ = c.computeVar(&value, envVarRegExp, 2, 1, func(varName *string) string {
return os.Getenv(*varName)
})

That means you could add in a config file values based on the name of environment variable, which would allow you to use said environment variable to modify the config.

See an example in the revel/config REAMD.md file.

[DEFAULT]
host: www.example.com
protocol: http://
base-url: %(protocol)s%(host)s    <====

The OP pveyes points out to this comment in type.go:

// substitute by new value and take off leading '%(' and trailing ')s'
// %(foo)s => headsz=2, tailsz=2
// ${foo} => headsz=2, tailsz=1

So:

> - when using environment variables use ${ENV_VARS}, and

  • for unfolding variables use %(UNF_VARS)s

huangapple
  • 本文由 发表于 2014年5月18日 02:25:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/23714707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定