Revel Go框架中除了app.conf和环境变量之外的外部配置方式是什么?

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

External configuration beside app.conf & environment variables for revel go framework

问题

我已经阅读了有关自定义配置和环境变量的revel app.conf手册,但是我找不到在app.conf中使用额外外部配置的方法。

我的目标是在内部的app.conf之外实现外部配置文件。比如说创建一个名为example的产品,example产品通过example.conf(默认位置可以是/etc/example/example.conf)向用户公开配置属性,而不是通过app.conf(不向最终用户公开)。

例如:app.conf中的http配置字段

http.addr =
http.port = 9000

扩展到example.conf

http.addr =
http.port = 9000

[database]
host = "localhost"
port = 8080
user = "username"
password = "password"
# etc...

然后,在应用程序启动时读取example.conf,使用其中的值,并将这些值应用于app.conf之上(覆盖)。最后,revel服务器启动!

如何在revel go框架中实现这个目标?

英文:

I have read revel app.conf manual for custom configuration and environment variables. however I couldn't find way to use additional external configuration along with app.conf.

My goal is to achieve external configuration file in addition to internal app.conf. Let's say creating a product called example and example product maintains it's sensible defaults with app.conf (not exposing to end user) instead product exposes config attributes via example.conf (default location could be /etc/example/example.conf) for product users.

For example: http config field from app.conf

http.addr =
http.port = 9000

extend it to example.conf

http.addr =
http.port = 9000

[database]
host = "localhost"
port = 8080
user = "username"
password = "password"
# etc...

Then I read example.conf during an application start use values also apply values on top of app.conf (overriding). Finally revel server starts!

How to achieve this goal with revel go framework?

答案1

得分: 1

看起来你正在针对app.conf的设计进行操作。它已经设置为分节,例如所有这些内容都在一个单独的app.conf文件中:

[dev]
results.pretty = true
watch = true
http.addr = 192.168.1.2

[test]
results.pretty = true
watch = true
http.addr = 192.168.1.22

[prod]
results.pretty = false
watch = false
http.addr = 192.168.1.100

你可以通过使用三个不同的命令行选项来启动三个不同的场景:

revel run bitbucket.org/mycorp/my-app dev

revel run bitbucket.org/mycorp/my-app test

revel run bitbucket.org/mycorp/my-app prod

我知道这不完全符合你的目标,但你可以实现类似的结果。

英文:

It appears you are working against the design of the app.conf. It is already setup to be sectioned, for example all this is in a single app.conf file

[dev]
results.pretty = true
watch = true
http.addr = 192.168.1.2

[test]
results.pretty = true
watch = true
http.addr = 192.168.1.22

[prod]
results.pretty = false
watch = false
http.addr = 192.168.1.100

you can launch 3 different scenarios by using three different command line options

revel run bitbucket.org/mycorp/my-app dev

revel run bitbucket.org/mycorp/my-app test

revel run bitbucket.org/mycorp/my-app prod

I know this is not exactly what your goal is but you can acheive a similar result.

答案2

得分: 1

在github.com/revel/revel/revel.go的第152行左右,你有这样的代码:

Config, err = LoadConfig("app.conf")

也许你可以尝试修改为以下代码:

if len(os.Getenv("SOME ENV VAR")) > 0 {
    Config, err = LoadConfig("path/to/your/example.conf")
} else {
    Config, err = LoadConfig("app.conf")
}

你只需要在你的生产服务器上设置环境变量。

这样你就不会使用app.conf,而是使用example.conf。

英文:

In github.com/revel/revel/revel.go around line 152 you have something like

Config, err = LoadConfig("app.conf").

Maybe you can try and modify that with this

if len(os.Getenv("SOME ENV VAR")) > 0 {
    Config, err = LoadConfig("path/to/your/example.conf")
} else {
    Config, err = LoadConfig("app.conf")
}

You just need to set env var on your prod server.

That way you will not be using app.conf but your example.conf.

huangapple
  • 本文由 发表于 2015年8月22日 04:38:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/32148903.html
匿名

发表评论

匿名网友

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

确定