1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 08:19:15 +08:00

feat: 修改读取配置文件方法

This commit is contained in:
zhengkunwang223 2023-02-10 14:22:37 +08:00 committed by zhengkunwang223
parent 7c20fddd21
commit 9c1fef0309

View File

@ -3,6 +3,8 @@ package viper
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/utils/files"
"path"
"strings" "strings"
"github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/cmd"
@ -15,19 +17,29 @@ import (
) )
func Init() { func Init() {
stdout, err := cmd.Exec("grep '^BASE_DIR=' /usr/bin/1pctl | cut -d'=' -f2") baseDir := "/opt"
if err != nil { fileOp := files.NewFileOp()
panic(err)
}
baseDir := strings.ReplaceAll(stdout, "\n", "")
if len(baseDir) == 0 {
panic("error `BASE_DIR` find in /usr/bin/1pctl")
}
v := viper.NewWithOptions() v := viper.NewWithOptions()
v.SetConfigType("yaml") v.SetConfigType("yaml")
reader := bytes.NewReader(conf.AppYaml) if fileOp.Stat("/opt/1panel/conf/app.yaml") {
if err := v.ReadConfig(reader); err != nil { v.SetConfigName("app")
panic(fmt.Errorf("Fatal error config file: %s \n", err)) v.AddConfigPath(path.Join("/opt/1pane/conf"))
if err := v.ReadInConfig(); err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
} else {
stdout, err := cmd.Exec("grep '^BASE_DIR=' /usr/bin/1pctl | cut -d'=' -f2")
if err != nil {
panic(err)
}
baseDir = strings.ReplaceAll(stdout, "\n", "")
if len(baseDir) == 0 {
panic("error `BASE_DIR` find in /usr/bin/1pctl")
}
reader := bytes.NewReader(conf.AppYaml)
if err := v.ReadConfig(reader); err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
} }
v.OnConfigChange(func(e fsnotify.Event) { v.OnConfigChange(func(e fsnotify.Event) {
if err := v.Unmarshal(&global.CONF); err != nil { if err := v.Unmarshal(&global.CONF); err != nil {