1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 16:29:17 +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,20 +17,30 @@ import (
) )
func Init() { func Init() {
baseDir := "/opt"
fileOp := files.NewFileOp()
v := viper.NewWithOptions()
v.SetConfigType("yaml")
if fileOp.Stat("/opt/1panel/conf/app.yaml") {
v.SetConfigName("app")
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") stdout, err := cmd.Exec("grep '^BASE_DIR=' /usr/bin/1pctl | cut -d'=' -f2")
if err != nil { if err != nil {
panic(err) panic(err)
} }
baseDir := strings.ReplaceAll(stdout, "\n", "") baseDir = strings.ReplaceAll(stdout, "\n", "")
if len(baseDir) == 0 { if len(baseDir) == 0 {
panic("error `BASE_DIR` find in /usr/bin/1pctl") panic("error `BASE_DIR` find in /usr/bin/1pctl")
} }
v := viper.NewWithOptions()
v.SetConfigType("yaml")
reader := bytes.NewReader(conf.AppYaml) reader := bytes.NewReader(conf.AppYaml)
if err := v.ReadConfig(reader); err != nil { if err := v.ReadConfig(reader); err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err)) 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 {
panic(err) panic(err)