[英]How to read environmental variables in app.yaml?
I am using Google App Engine and have an app.yaml file that looks like this:我正在使用 Google App Engine 并有一个 app.yaml 文件,如下所示:
runtime: go115
env_variables:
INSTANCE_CONNECTION_NAME: secret:northamerica-northeast1:special
DB_USER: db_username
DB_PASS: p@ssword
DB_NAME: superduper
Using this Github as a refernce, I am using this to read those values:使用这个Github作为参考,我用它来读取这些值:
dbUser = mustGetenv("DB_USER")
dbUser = mustGetenv("DB_USER")
I do a gcloud app deploy and then check for the variable with the set command.我执行 gcloud 应用程序部署,然后使用 set 命令检查变量。 I do not see the variable there, and likewise, when my program runs it can not find the variable.
我在那里看不到变量,同样,当我的程序运行时,它找不到变量。
I seem to be doing everything the example is doing, but my variable is not being found by my program.我似乎正在做示例所做的一切,但我的程序没有找到我的变量。 Is there any specific formating of the yaml file?
yaml 文件有什么特定的格式吗? Is there some other step I need to be doing before trying to read the variable?
在尝试读取变量之前,我还需要做什么其他步骤吗?
您可以使用os.Getenv
读取变量。
dbUser = os.Getenv("DB_USER")
You can try using viper https://github.com/spf13/viper您可以尝试使用毒蛇https://github.com/spf13/viper
func Init() {
// Set the file name of the configurations file
viper.SetConfigName("app")
// Set the path to look for the configurations file
viper.AddConfigPath("./config")
// Enable VIPER to read Environment Variables
viper.AutomaticEnv()
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file, %s", err)
}
}
Add this initialisation in you main then you can access using following anywhere in code在您的 main 中添加此初始化,然后您可以在代码中的任何位置使用以下内容进行访问
viper.GetString("env_variables.INSTANCE_CONNECTION_NAME")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.