在revel app.conf中读取环境变量

Revel使用app.conf来存储配置。 我希望我的配置使用 os.Getenv(key)</ code> </ p>

从环境变量中获取值,我该怎么做? 我应该使用revel.Config进行更改吗? 如果可以,我应该在哪里放置它?</ p>

还是有其他方法?</ p>

我主要将其用于存储数据库信息(我不想 在配置文件中输入我的凭据)</ p>

谢谢</ p>
</ div>

展开原文

原文

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个回答

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
douxian6694
douxian6694 非常真实 我已将您的评论包含在答案中,以提高知名度。
大约 6 年之前 回复
douxiangshi6568
douxiangshi6568 谢谢,他们应该在自述文件中更加清楚,使用环境变量时,请使用$ {ENV_VARS},对于展开变量,请使用github.com/revel/config/blob/…中所述的%(UNF_VARS)s。
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐