将值传递给使用环境变量的App Engine上的Go应用程序

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

Passing values to a Go application on App Engine with Environment variables

问题

根据文档,你只需要将它们包含在app.yaml文件中即可。
https://cloud.google.com/appengine/docs/go/config/appconfig#Go_app_yaml_Defining_environment_variables

所以我像这样做了:

env_variables:
    DEVDOT_OAUTH_CLIENT_ID: 'xxxxx'
    DEVDOT_OAUTH_CLIENT_SECRET: 'xxxxxx'

但是当我的应用程序运行时,它并没有获取到这些值。我正在使用MVM运行时,所以我删除了我的init()函数,并尝试从main()函数和我分配给/_ah/start路径的处理程序中调用os.Getenv()。

有什么想法吗?

英文:

According to the docs, you just have to include them in the app.yaml file
https://cloud.google.com/appengine/docs/go/config/appconfig#Go_app_yaml_Defining_environment_variables

So I did that like this:

env_variables:
    DEVDOT_OAUTH_CLIENT_ID: 'xxxxx'
    DEVDOT_OAUTH_CLIENT_SECRET: 'xxxxxx'

But when my application runs, it doesn't get these values. I'm running on a MVM runtime so I removed my init() function and tried calling os.Getenv() from both main() and from a handler I had assigned to the /_ah/start path.

Any ideas?

答案1

得分: 1

根据https://stackoverflow.com/questions/29186031/how-to-set-environment-variables-on-google-appengine的解释,“环境变量在所有初始化函数运行之前都不可用,我认为全局变量声明也可能适用相同的情况。它在函数中起作用是因为在那个时候环境变量已经设置好了”。

你似乎在说不同的事情 - 你可以尝试一下我指向的另一个问题中的示例代码,当那个问题被回答时它是有效的,然后告诉我们你观察到的情况是什么?

英文:

As https://stackoverflow.com/questions/29186031/how-to-set-environment-variables-on-google-appengine explains, "environment variables are not ready until all the init functions are run and I would say same might be applying to the global variable declaration too. It works from a function because by that time environment variables are set".

You appear to be saying otherwise -- can you try the example code at the other Q I pointed to, which was working when that Q was answered, and tell us what you observe instead?

答案2

得分: 1

我认为你需要从app.yaml文件中获取环境变量。这对我有效:

  1. 执行以下命令安装godotenv:go install github.com/joho/godotenv/cmd/godotenv@latest。
  2. 安装完成后,在GOPATH目录的bin文件夹中会有一个.exe文件。
  3. 现在,在你的项目目录中执行以下操作:
    3.1. 使用命令godotenv -f ./app.yaml(或者你的app.yaml文件所在路径)。
    3.2. 使用命令go run .\cmd\(或者你的主文件所在路径)。
英文:

I think you have to get the env variables from the app.yaml file?. This works for me:

  1. Do this go install github.com/joho/godotenv/cmd/godotenv@latest.
  2. Once you install that you will have an .exe file in bin in the GOPATH directory.
  3. Now you just to do that in your directory where you have your project:
    3.1. godotenv -f ./app.yaml(or where you have your app.yaml file) go run .\cmd.(or path where you have your main file)

huangapple
  • 本文由 发表于 2015年12月15日 05:47:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/34277201.html
匿名

发表评论

匿名网友

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

确定