如何设置GAE特定环境的环境变量?

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

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是一个公开的变量,你可以使用自己的逻辑来设置它。有多种方法可以做到这一点:

  1. MARTINI_ENV不存在时,默认将martini.Env设置为生产环境。
  2. 在你的代码库中添加自己的config.yaml文件,解析它并从中设置martini.Env
  3. 使用像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:

  1. Default setting martini.Env to production when MARTINI_ENV is not present
  2. Add your own config.yaml to your repo, parse it and set martini.Env from there
  3. 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 现在似乎支持环境变量了。

https://cloud.google.com/appengine/docs/go/config/appconfig#Go_app_yaml_Defining_environment_variables

英文:

GAE Go does seem to be supporting environment variables now

https://cloud.google.com/appengine/docs/go/config/appconfig#Go_app_yaml_Defining_environment_variables

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

发表评论

匿名网友

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

确定