1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-02-08 17:40:08 +08:00
1Panel/agent/init/viper/viper.go

66 lines
1.5 KiB
Go
Raw Normal View History

2024-07-23 14:48:37 +08:00
package viper
import (
"bytes"
"fmt"
"path"
"github.com/1Panel-dev/1Panel/agent/cmd/server/conf"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/files"
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
2024-07-23 14:48:37 +08:00
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)
func Init() {
mode := ""
fileOp := files.NewFileOp()
v := viper.NewWithOptions()
v.SetConfigType("yaml")
config := global.ServerConfig{}
2024-07-23 14:48:37 +08:00
if err := yaml.Unmarshal(conf.AppYaml, &config); err != nil {
panic(err)
}
if config.Base.Mode != "" {
mode = config.Base.Mode
2024-07-23 14:48:37 +08:00
}
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") {
v.SetConfigName("app")
v.AddConfigPath(path.Join("/opt/1panel/conf"))
if err := v.ReadInConfig(); err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
} else {
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) {
if err := v.Unmarshal(&global.CONF); err != nil {
panic(err)
}
})
serverConfig := global.ServerConfig{}
2024-07-23 14:48:37 +08:00
if err := v.Unmarshal(&serverConfig); err != nil {
panic(err)
}
global.CONF = serverConfig
global.CONF.Base.IsDemo = v.GetBool("system.is_demo")
initInstallDir()
2024-07-23 14:48:37 +08:00
global.Viper = v
}
func initInstallDir() {
_, nodeInfo, err := xpack.LoadNodeInfo()
2024-07-23 14:48:37 +08:00
if err != nil {
panic(err)
}
global.CONF.Base.InstallDir = nodeInfo.BaseDir
2024-07-23 14:48:37 +08:00
}