Closed choyri closed 1 day ago
不可以喔 要以配置文件为主,要在线修改的
On Wed, Nov 13, 2024 at 21:34 Chotow @.***> wrote:
@.**** commented on this pull request.
In model/config.go https://github.com/naiba/nezha/pull/474#discussion_r1840289814:
}
// Read 读取配置文件并应用 func (c *Config) Read(path string) error {
- c.v = viper.New()
- c.v.SetConfigFile(path)
- err := c.v.ReadInConfig()
- c.k = koanf.New(".")
- c.filePath = path
- if _, err := os.Stat(path); err == nil {
- err = c.k.Load(file.Provider(path), yaml.Parser())
- if err != nil {
- return err
- }
- }
- err := c.k.Load(env.Provider("NZ_", ".", func(s string) string {
根据 koanf 的文档描述,按加载顺序;在这个修改中,如果目标配置文件存在,会先加载,而后再加载环境变量,因此,环境变量优先级更高。
koanf does not impose any ordering on loading config from various providers. Every successive Load() or Merge() merges new config into the existing config. That is, it is possible to load environment variables first, then files on top of it, and then command line variables on top of it, or any such order.
— Reply to this email directly, view it on GitHub https://github.com/naiba/nezha/pull/474#discussion_r1840289814, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG7DUMP7B4TC22NGCTK34PL2ANIM5AVCNFSM6AAAAABRQABGWGVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDIMZTGE3DKMRUGA . You are receiving this because you commented.Message ID: @.***>
不可以喔 要以配置文件为主,要在线修改的
一般情况下不会有同时使用配置文件和环境变量的朋友吧?于我而言,是计划作为云服务部署的,只会配置环境变量,不会有配置文件存在,也不会在线修改配置。
不过这个服务既然支持在线修改配置,那确实也是可以这样定义优先级。
The format of environment variables is as follows:
In addition, it doesn't panic even if the config file doesn't exist, so we can start it more simply as a cloud service.