组织环境变量 Golang

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

Organizing Environment Variables Golang

问题

在Node.js中,我使用nconf模块来存储每个项目的环境变量,如S3密钥、GCM密钥等。

我还没有找到Go中类似的解决方案。

一般接受的工具是用来帮助管理每个Go项目的环境变量的?

提前感谢。

英文:

In Node.js I use the nconf module to house environment variables like S3 keys, GCM keys, etc for each of my projects.

I haven't been able to find a similar solution in Go.

What are the generally accepted tools to help manage environment variables for each Go project?

Thanks in advance.

答案1

得分: 28

我强烈推荐使用github.com/namsral/flag
它类似于内置的flag,但你还可以通过环境变量来提供参数。

例如,假设你有以下代码:

package main

import "fmt"
import "github.com/namsral/flag"

func main() {
    var port = 3000
    flag.IntVar(&port, "port", port, "端口号")
    flag.Parse()
    fmt.Println("你似乎更喜欢", port)
}

然后你可以通过命令行选项或环境变量来提供值:

:~/dev/GO$ go run dummy.go
你似乎更喜欢 3000
:~/dev/GO$ go run dummy.go -port=1234
你似乎更喜欢 1234
:~/dev/GO$ PORT=4321 go run dummy.go
你似乎更喜欢 4321
:~/dev/GO$ PORT=4321 go run dummy.go -port=5555
你似乎更喜欢 5555

当很难提供命令行参数时,这可能很重要。例如,如果你使用gin自动重启服务器,你无法提供命令行参数,因为gin只是在没有传递任何参数的情况下调用主代码的go run

英文:

I would strongly recommend using github.com/namsral/flag instead.
It's like the built in flag except you can also supply the parameters via environment variables.

For example, suppose you have this code:

package main

import "fmt"
import "github.com/namsral/flag"

func main() {
    var port = 3000
	flag.IntVar(&port, "port", port, "Port number")
    flag.Parse()
	fmt.Println("You seem to prefer", port)
}

Then you can supply the values with either a command line option or an environment variable:

:~/dev/GO$ go run dummy.go
You seem to prefer 3000
:~/dev/GO$ go run dummy.go -port=1234
You seem to prefer 1234
:~/dev/GO$ PORT=4321 go run dummy.go
You seem to prefer 4321
:~/dev/GO$ PORT=4321 go run dummy.go -port=5555
You seem to prefer 5555

This might matter when it's hard to supply command line args. For example, if you use gin to automatically restart a server you have no way to supply command line arguments since gin is just calling go run on the main code without any arguments passed along.

答案2

得分: 12

我之前在开始学习Go语言时阅读了一些相关资料。根据这个链接http://peter.bourgon.org/go-in-production/,他们推荐使用命令行标志(参数)而不是环境变量,甚至将环境变量转换为命令行标志来使用他们的命令行应用程序。

虽然需要一些时间来适应,但我确实看到了在开发、演示和生产环境之间使用纯命令行标志的优势,每个环境都有特定的脚本。

例如,这是我最近编写的一个小型Web应用程序:

// 全局标志
var isdebug bool
var port int
var cert string
var key string
var dbdsn string
var dbmaxidle int
var dbmaxopen int
var imguri string

// init是整个Web应用程序的入口点。
func init() {

log.Println("Starting wwwgo ...")

// 设置标志
//flag.StringVar(&host, "host", "", "Specify a host to redirect to. Use this to redirect all traffic to a single url.")
flag.IntVar(&port, "port", 8080, "Specify the port to listen to.")
flag.BoolVar(&isdebug, "isdebug", false, "Set to true to run the app in debug mode.  In debug, it may panic on some errors.")
flag.StringVar(&cert, "cert", "", "Enables listening on 443 with -cert and -key files specified.  This must be a full path to the certificate .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.")
flag.StringVar(&key, "key", "", "Enables listening on 443 with -cert and -key files specified.  This must be a full path to the key .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.")
flag.StringVar(&dbdsn, "dbdsn", "root:root@tcp(localhost:3306)/dev_db?timeout=5s&tls=false&autocommit=true", "Specifies the MySql DSN connection.")
flag.IntVar(&dbmaxidle, "dbmaxidle", 0, "Sets the database/sql MaxIdleConns.")
flag.IntVar(&dbmaxopen, "dbmaxopen", 500, "Sets the database/sql MaxOpenConns.")
flag.StringVar(&imguri, "imguri", "/cdn/uploads/", "Set this to the full base uri of all images, for example on a remote CDN server or local relative virtual directory.")
flag.Parse()

// 记录我们的标志
if isdebug != false {
    log.Println("DEBUG mode enabled")
}
if cert != "" && key != "" {
    log.Println("Attempting SSL binding with supplied cert and key.")
}
if dbdsn != "" {
    log.Printf("Using default dbdsn: %s", dbdsn)
}

...

}

这在演示/生产环境中非常方便。

快速运行./wwwgo -h命令以获取“这个参数是什么意思?”的完整文档:

admin@dev01:~/code/frontend/src/wwwgo [master]$ ./wwwgo -h
Usage of ./wwwgo:
-cert="": Enables listening on 443 with -cert and -key files specified. This must be a full path to the certificate .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.
-dbdsn="root:root@tcp(localhost:3306)/dev_db?timeout=5s&tls=false&autocommit=true": Specifies the MySql DSN connection.
-dbmaxidle=0: Sets the database/sql MaxIdleConns.
-dbmaxopen=500: Sets the database/sql MaxOpenConns.
-imguri="/cdn/uploads/": Set this to the full base uri of all images, for example on a remote CDN server or local relative virtual directory.
-isdebug=false: Set to true to run the app in debug mode. In debug, it may panic on some errors.
-key="": Enables listening on 443 with -cert and -key files specified. This must be a full path to the key .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.
-port=8080: Specify the port to listen to.

在命令行中有这么多选项,而且不需要文档说明-它们已经内置在flags包中。

你可以清楚地看到默认值。

对于这种类型的文档,我倾向于为团队使用的常见“开发环境”设置所有默认值。我们都可以在本地数据库中使用root/root访问权限。我们在开发过程中都使用8080端口运行这个特定的Web应用程序,等等。这样,你只需要运行:

go build
./wwwgo

应用程序将使用所有默认值运行-这些默认值已经有文档说明了。在生产环境中,只需覆盖默认值。内置的标志解析器将在参数格式错误时使应用程序发生panic,这也非常好。

英文:

I did some reading on this a while back when I was getting started with Go. According to this link, http://peter.bourgon.org/go-in-production/, they recommend using CLI flags (parameters) instead of environment vars - they even convert environment vars to flags to their CLI apps.

It took some getting used to; but, I really do see the advantages of going pure CLI flags between development, staging and production environments - having specific scripts for each environment.

For example, here's a little web app I wrote recently:

// global flags
var isdebug bool
var port int
var cert string
var key string
var dbdsn string
var dbmaxidle int
var dbmaxopen int
var imguri string

// init is the entry point for the entire web application.
func init() {

	log.Println("Starting wwwgo ...")

	// setup the flags
	//flag.StringVar(&host, "host", "", "Specify a host to redirect to. Use this to redirect all traffic to a single url.")
	flag.IntVar(&port, "port", 8080, "Specify the port to listen to.")
	flag.BoolVar(&isdebug, "isdebug", false, "Set to true to run the app in debug mode.  In debug, it may panic on some errors.")
	flag.StringVar(&cert, "cert", "", "Enables listening on 443 with -cert and -key files specified.  This must be a full path to the certificate .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.")
	flag.StringVar(&key, "key", "", "Enables listening on 443 with -cert and -key files specified.  This must be a full path to the key .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.")
	flag.StringVar(&dbdsn, "dbdsn", "root:root@tcp(localhost:3306)/dev_db?timeout=5s&tls=false&autocommit=true", "Specifies the MySql DSN connection.")
	flag.IntVar(&dbmaxidle, "dbmaxidle", 0, "Sets the database/sql MaxIdleConns.")
	flag.IntVar(&dbmaxopen, "dbmaxopen", 500, "Sets the database/sql MaxOpenConns.")
	flag.StringVar(&imguri, "imguri", "/cdn/uploads/", "Set this to the full base uri of all images, for example on a remote CDN server or local relative virtual directory.")
	flag.Parse()

	// log our flags
	if isdebug != false {
		log.Println("DEBUG mode enabled")
	}
	if cert != "" && key != "" {
		log.Println("Attempting SSL binding with supplied cert and key.")
	}
	if dbdsn != "" {
		log.Printf("Using default dbdsn: %s", dbdsn)
	}
	
	...
}

This really becomes nice in staging/production environments.

A quick ./wwwgo -h for "what the heck was that parameter?" gives you full documentation:

admin@dev01:~/code/frontend/src/wwwgo [master]$ ./wwwgo -h
Usage of ./wwwgo:
  -cert="": Enables listening on 443 with -cert and -key files specified.  This must be a full path to the certificate .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.
  -dbdsn="root:root@tcp(localhost:3306)/dev_db?timeout=5s&tls=false&autocommit=true": Specifies the MySql DSN connection.
  -dbmaxidle=0: Sets the database/sql MaxIdleConns.
  -dbmaxopen=500: Sets the database/sql MaxOpenConns.
  -imguri="/cdn/uploads/": Set this to the full base uri of all images, for example on a remote CDN server or local relative virtual directory.
  -isdebug=false: Set to true to run the app in debug mode.  In debug, it may panic on some errors.
  -key="": Enables listening on 443 with -cert and -key files specified.  This must be a full path to the key .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.
  -port=8080: Specify the port to listen to.

Very nice to have many options at CLI, and no documentation required - it's built into the flags package.

You can clearly see the defaults immediately.

With this type of documentation, I tend to setup all the defaults for common "development environments" that the team uses. We all have root/root access to our local databases. We all are using port 8080 for this particular web app during development, etc. That way, you just have to run:

go build
./wwwgo

And the app runs with all defaults - defaults that are documented. In production, just override the defaults. The built-in flag parsers will panic the application if any parameters are in the wrong format, which is also very nice.

答案3

得分: 4

我们已经将其用于大规模微服务应用程序。这不涉及使用任何第三方库,只是使用纯粹的Go语言和反射。它非常简单易用,并遵循DRY原则。添加新的环境变量和配置默认值非常容易。您还可以通过更改仅两行代码来设置从配置文件加载作为备用。请查看github页面获取更多信息。

package config

import (
	"fmt"
	"os"
	"reflect"
)

/* 从环境变量加载配置的标签名称 */
const (
	ENV     = "env"
	DEFAULT = "default"
)

type Configuration struct {
	Port        string `env:"port" default:"3009"`
	MongoURL    string `env:"MongoUrl" default:"mongodb://localhost:27017/test"`
	UserService string `env:"UserService" default:"http://localhost:3005"`
	AuthService string `env:"AuthService" default:"http://localhost:3050"`
	Debug       string `env:"Debug" default:"true"`
}

/* 为了避免意外覆盖,不导出的实例 */
var serviceConfig Configuration

func setConfig() {
	// ValueOf返回表示运行时数据的Value
	v := reflect.ValueOf(serviceConfig)
	for i := 0; i < v.NumField(); i++ {
		// 获取字段标签值
		tag := v.Type().Field(i).Tag.Get(ENV)
		defaultTag := v.Type().Field(i).Tag.Get(DEFAULT)

		// 如果标签未定义或被忽略,则跳过
		if tag == "" || tag == "-" {
			continue
		}
		a := reflect.Indirect(reflect.ValueOf(serviceConfig))
		EnvVar, Info := loadFromEnv(tag, defaultTag)
		if Info != "" {
			fmt.Println("缺少环境配置 '" + a.Type().Field(i).Name + "', 加载默认设置!")
		}
		/* 将环境变量的值设置为相应的结构字段 */
		reflect.ValueOf(&serviceConfig).Elem().Field(i).SetString(EnvVar)
	}
}

func loadFromEnv(tag string, defaultTag string) (string, string) {
	/* 检查环境中是否定义了标签,否则替换为默认值 */
	envVar := os.Getenv(tag)
	if envVar == "" {
		envVar = defaultTag
		/* '1'用于指示正在加载默认值 */
		return envVar, "1"
	}
	return envVar, ""
}

/* GetConfiguration:导出的函数,返回配置实例的副本 */
func GetConfiguration() Configuration {
	return serviceConfig
}

func init() {
	setConfig()
	fmt.Printf("服务配置: %+v\n ", serviceConfig)
}
英文:

We have used this for a large scale microservice application. This doesn't involve use of any third party libraries, just plain go lang using reflection. It is very simple to use and follows DRY principle. Adding new environment variables and configuring defaults is quite easy. You can also set loading from a config file as a fallback by changing just 2 lines of code. Checkout github page for more info.

package config
import (
&quot;fmt&quot;
&quot;os&quot;
&quot;reflect&quot;
)
/* Tag names to load configuration from environment variable */
const (
ENV     = &quot;env&quot;
DEFAULT = &quot;default&quot;
)
type Configuration struct {
Port        string `env:&quot;port&quot; default:&quot;3009&quot;`
MongoURL    string `env:&quot;MongoUrl&quot; default:&quot;mongodb://localhost:27017/test&quot;`
UserService string `env:&quot;UserService&quot; default:&quot;http://localhost:3005&quot;`
AuthService string `env:&quot;AuthService&quot; default:&quot;http://localhost:3050&quot;`
Debug       string `env:&quot;Debug&quot; default:&quot;true&quot;`
}
/* Non-exported instance to avoid accidental overwrite */
var serviceConfig Configuration
func setConfig() {
// ValueOf returns a Value representing the run-time data
v := reflect.ValueOf(serviceConfig)
for i := 0; i &lt; v.NumField(); i++ {
// Get the field tag value
tag := v.Type().Field(i).Tag.Get(ENV)
defaultTag := v.Type().Field(i).Tag.Get(DEFAULT)
// Skip if tag is not defined or ignored
if tag == &quot;&quot; || tag == &quot;-&quot; {
continue
}
a := reflect.Indirect(reflect.ValueOf(serviceConfig))
EnvVar, Info := loadFromEnv(tag, defaultTag)
if Info != &quot;&quot; {
fmt.Println(&quot;Missing environment configuration for &#39;&quot; + a.Type().Field(i).Name + &quot;&#39;, Loading default setting!&quot;)
}
/* Set the value in the environment variable to the respective struct field */
reflect.ValueOf(&amp;serviceConfig).Elem().Field(i).SetString(EnvVar)
}
}
func loadFromEnv(tag string, defaultTag string) (string, string) {
/* Check if the tag is defined in the environment or else replace with default value */
envVar := os.Getenv(tag)
if envVar == &quot;&quot; {
envVar = defaultTag
/* &#39;1&#39; is used to indicate that default value is being loaded */
return envVar, &quot;1&quot;
}
return envVar, &quot;&quot;
}
/*GetConfiguration :Exported function to return a copy of the configuration instance */
func GetConfiguration() Configuration {
return serviceConfig
}
func init() {
setConfig()
fmt.Printf(&quot;Service configuration : %+v\n &quot;, serviceConfig)
}

答案4

得分: 1

我更喜欢使用go-arg来设置环境变量。它易于使用且具有良好的功能。

例如:


package configs

import (
	"fmt"
	"github.com/alexflint/go-arg"
)

type config struct {
	 DBHost string `arg:"env:DBHost, -D, --dbhost" help:"数据库主机" placeholder:"DBHost"`
}

var Config config

func main(){

  arg.MustParse(&Config)
  fmt.Println(Config.DBHost)

}

使用这个库,你可以从环境变量中获取变量,也可以通过参数传递。

export DBHost=127.0.0.1

或者

go run ./main.go --dbhost=127.0.0.1

英文:

Well I prefer go-arg for setting environment variables. It is easy to use and has nice features.

For example:


package configs

import (
	&quot;fmt&quot;
	&quot;github.com/alexflint/go-arg&quot;
)

type config struct {
	 DBHost string `arg:&quot;env:DBHost, -D, --dbhost&quot; help:&quot;Host of the database&quot; placeholder:&quot;DBHost&quot;`
}

var Config config

func main(){

  arg.MustParse(&amp;Config)
  fmt.Println(Config.DBHost)

}

With this library either you can take the variable from your env or you can pass through args.

export DBHost=127.0.0.1

or

go run ./main.go --dbhost=127.0.0.1

答案5

得分: 0

在Node.js中,nconf是一个很棒的工具。它不仅可以用于存储秘钥。

在Golang中,据我所知有两个很棒的包可以方便地使用**.env文件,分别是godotenvviper,我更喜欢使用godotenv**,因为它更容易使用。

  • 优点: 开发人员无法看到你的生产秘钥。你可以在开发、测试和生产环境中使用不同的秘钥,而无需修改代码。
  • 缺点: 恶意代码可能会读取你的秘钥。你的应用程序代码的大部分可能是开源库。糟糕的代码可能会悄悄地插入其中,而你却不知道。

以下是使用godotenv的示例:

首先,在终端中运行以下命令:

go get github.com/joho/godotenv

在你的.env文件中:

S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE

在你的main.go文件中:

package main
import (
"github.com/joho/godotenv"
"log"
"os"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
s3Bucket := os.Getenv("S3_BUCKET")
secretKey := os.Getenv("SECRET_KEY")
// 现在可以使用s3或其他操作了
}
英文:

In nodejs, nconf is a great tool. It gives more than storing secret key.<br>
In Golang, as far as i know there is two great package to use .env file easily , godotenv and viper , I prefer godotenv beacuse it's much easier .<br>

  • Pros: Developers won't see your production secrets. You can use different secrets in dev, test, and production, without having to modify the code.
  • Cons: Malicious code can read your secrets. The bulk of your application's code is probably open-source libraries. Bad code may creep in without you knowing it.

Example for using godotenv

First run this command inside your terminal,

go get github.com/joho/godotenv

In your .env file

S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE

In your main.go file

package main
import (
&quot;github.com/joho/godotenv&quot;
&quot;log&quot;
&quot;os&quot;
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal(&quot;Error loading .env file&quot;)
}
s3Bucket := os.Getenv(&quot;S3_BUCKET&quot;)
secretKey := os.Getenv(&quot;SECRET_KEY&quot;)
// now do something with s3 or whatever
}

huangapple
  • 本文由 发表于 2014年7月22日 04:15:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/24873883.html
匿名

发表评论

匿名网友

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

确定