从文件中读取环境变量并将其存储为全局变量,这样做可以吗?

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

Is it okay to store enviornment variables as global variables by reading from file once?

问题

我有一个config.env文件,其中包含一个SecretKey,每次我想要解析/读取JWT(令牌)时,我都需要这个密钥的值。
所以,我考虑了2种可能的方法:
1)在代码(读取.env文件)执行时将SecretKey存储为全局变量,以便以后使用。
我不知道这是否会完全打破使用.env文件的目的

2)每次我想要解析令牌时都从文件中读取。(几乎每个API调用都会这样)

那么,选择1还是选择2?
或者完全使用其他方法?

附言:我想明确一下,我不是在问我是否可以用全局变量替换env,而是在问我是否可以在代码部署/执行后将SecretKey存储为全局变量。

英文:

I have a config.env file which has a SecretKey, I need the value of this key everytime I want to parse/read from JWT (token).
So, I have thought of 2 possible ways:

  1. Store the SecretKey as a global variable when the code(.env file reading) executes so it can be used later
    I don't know if that will beat the entire purpose of having an .env file

  2. Read from file everytime I want to parse the token. (which will be almost every API call)

So, 1 or 2?
Or something else entirely?

P.S. Just to be clear I am not asking if I can replace env with global, but whether I can store the SecretKey as a global variable once the code is deployed/executed.

答案1

得分: 1

是的,你可以存储它,为什么不能呢?

而且,不需要每次需要时都读取/解析文件是一个(非常)好的主意,这样做会使你的 API 调用变慢(具体取决于它们的实际操作)。

是否需要或应该使用全局变量是另一个问题。一般来说,不鼓励使用全局变量。尽管如此,这可能是一个很好的例外,尽管你也可以选择以其他方式存储它,例如在某种应用程序上下文中,或者在实现你的端点的处理程序的字段中。

英文:

Yes, you can store it, why couldn't you?

And yes, it's a (very) good idea not to read / parse the file every time you need it, that would (could) perceptibly make your API calls slower (depending on what they actually do).

Whether you need or should use a global variable is another question. Generally, global variables are discouraged. This may be a good exception, although you may choose to store it in another ways, e.g. in some kind of application context, or in fields of the handler that implement your endpoints.

答案2

得分: 0

关于第一项

一般来说,应该避免使用全局变量,因为这会使代码的可重用性降低,测试变得更加困难。可以说它与那个全局变量耦合在一起。它还限制了代码一次只能使用一个值的情况:如果需要使用两个不同的SecretKey值怎么办?
为了_解耦_代码,可以注入配置值(例如使用结构体或闭包)。

例如,以下功能与全局变量解耦,可以作为一个可重用的包/模块:

type MyApiCaller struct {
    SecretKey string
}

func (m *MyApiCaller) DoSomething() {
    fmt.Println("Do something with", m.SecretKey)
}

现在它可以在任何地方使用,甚至作为多个全局变量:

var Caller1 = &MyApiCaller{"secret1"}
var Caller2 = &MyApiCaller{"secret2"}

这也很容易进行测试,因为状态不是通过全局变量共享的。

关于第二项

每次需要值时读取配置文件可能会限制程序的性能,因为磁盘访问非常昂贵。但好处是程序可以在不重新启动的情况下获取最新的配置。

英文:

Regarding item 1

In general, global variables should be avoided because makes the code less reusable and more difficult to test. Let's say it is coupled to that global variable. It also limits the code to be used with one only value at a time: What if there is a need to use two different SecretKey values?
To decouple the code, the configuration value can be injected (for example using a struct or a closure).

For example, the following functionality is decoupled from global variables and it could be a package/module ready to reuse:

type MyApiCaller struct {
    SecretKey string
}

func (m *MyApiCaller) DoSomething() {
    fmt.Println("Do something with", m.SecretKey)
}

Now it can be used anywhere, even as multiple global variables:

var Caller1 = &MyApiCaller{"secret1"}
var Caller2 = &MyApiCaller{"secret2"}

It is also easy to test because the state is not shared through a global variable.

Regarding item 2

Reading the configuration file each time the value is needed will probably limit the program performance as disk access is very expensive. The lateral advantage is that the program would get the latest config without restart.

huangapple
  • 本文由 发表于 2021年7月28日 02:55:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/68550378.html
匿名

发表评论

匿名网友

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

确定