英文:
How to set GAE environment-specific environment variables?
问题
我有一个使用martini的Go语言GAE应用程序。我需要能够设置MARTINI_ENV
环境变量,以告诉martini它应该使用生产设置进行初始化。根据Python文档,你可以在app.yaml中设置环境变量。我在Go文档中没有看到任何提及,但我猜应该可以使用相同的方法。
我需要能够将MARTINI_ENV
环境变量设置为production
,但我只想在实际运行在生产环境时才这样做(即!appengine.IsDevAppServer()
)。有没有办法告诉app.yaml
只在非开发服务器上运行时执行这个操作?
英文:
I've got a GAE application in Go using martini. I need to be able to set the MARTINI_ENV
environment variable to tell martini that it should initialize with production settings. According to the Python docs you can set environment variables in the app.yaml. I didn't see any mention of this in the Go docs, but I'm guessing it should work the same.
I need to be able to set the MARTINI_ENV
environment variable to production
, but I only want to do that when I'm actually in production (i.e. !appengine.IsDevAppServer()
). Is there any way to tell app.yaml
to only do this in running on the non-dev server?
答案1
得分: 4
GAE在Go语言中没有设置环境变量的概念,因为这些变量不会在GAE实例之间共享。
不过,由于martini.Env
是一个公开的变量,你可以使用自己的逻辑来设置它。有多种方法可以做到这一点:
- 当
MARTINI_ENV
不存在时,默认将martini.Env
设置为生产环境。 - 在你的代码库中添加自己的
config.yaml
文件,解析它并从中设置martini.Env
。 - 使用像godotenv这样的库,使用其
Read
函数,该函数将读取你的dotfile而不是将其加载到环境中。
英文:
GAE in Go has no concept for setting environment variables since these won't be shared across your GAE instances.
Since martini.Env
is an exported variable though you are able to set it using your own logic. There are multiple ways to do this:
- Default setting
martini.Env
to production whenMARTINI_ENV
is not present - Add your own
config.yaml
to your repo, parse it and setmartini.Env
from there - Use a library like godotenv with its
Read
function, which will read your dotfile instead of loading it into the env.
答案2
得分: 1
GAE Go 现在似乎支持环境变量了。
英文:
GAE Go does seem to be supporting environment variables now
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论