How to read environmental variables in app.yaml?

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

How to read environmental variables in app.yaml?

问题

我正在使用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

根据这个Github作为参考,我正在使用以下代码来读取这些值:

dbUser = mustGetenv("DB_USER")

我执行了gcloud app deploy命令,并使用set命令检查变量。但是我没有在那里看到该变量,同样,当我的程序运行时也找不到该变量。

我似乎在按照示例的做法进行操作,但是我的程序找不到该变量。在尝试读取变量之前,是否需要对yaml文件进行特定的格式化?是否还有其他步骤我需要完成?

英文:

I am using Google App Engine and have an app.yaml file that looks like this:

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:

> dbUser = mustGetenv("DB_USER")

I do a gcloud app deploy and then check for the variable with the set command. 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? Is there some other step I need to be doing before trying to read the variable?

答案1

得分: 1

你可以使用os.Getenv来读取变量。

dbUser = os.Getenv("DB_USER")

并且你需要引用你的值。

env_variables:
  INSTANCE_CONNECTION_NAME: 'secret:northamerica-northeast1:special'
  DB_USER: 'db_username'
  DB_PASS: 'p@ssword'
  DB_NAME: 'superduper'
英文:

You can read variables with os.Getenv.

dbUser = os.Getenv("DB_USER")

And you have to quote your values.

env_variables:
  INSTANCE_CONNECTION_NAME: 'secret:northamerica-northeast1:special'
  DB_USER: 'db_username'
  DB_PASS: 'p@ssword'
  DB_NAME: 'superduper'

答案2

得分: 0

你可以尝试使用viper库来实现这个功能。以下是代码示例:

func Init() {
  // 设置配置文件的文件名
  viper.SetConfigName("app")

  // 设置查找配置文件的路径
  viper.AddConfigPath("./config")

  // 启用VIPER读取环境变量
  viper.AutomaticEnv()

  viper.SetConfigType("yaml")

  if err := viper.ReadInConfig(); err != nil {
    fmt.Printf("读取配置文件出错,%s", err)
  }
}

将这个初始化代码添加到你的主函数中,然后你可以在代码的任何地方使用以下方式访问配置项:

viper.GetString("env_variables.INSTANCE_CONNECTION_NAME")

你可以在这里找到viper库的详细信息:https://github.com/spf13/viper

英文:

You can try using 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

viper.GetString("env_variables.INSTANCE_CONNECTION_NAME")

huangapple
  • 本文由 发表于 2021年11月16日 11:08:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/69983324.html
匿名

发表评论

匿名网友

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

确定