2022-08-16 23:30:23 +08:00
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
2023-01-30 21:05:20 +08:00
|
|
|
"bytes"
|
2022-08-16 23:30:23 +08:00
|
|
|
"fmt"
|
2023-02-10 14:22:37 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
"path"
|
2023-02-01 16:46:13 +08:00
|
|
|
"strings"
|
2023-01-06 18:53:25 +08:00
|
|
|
|
2023-02-09 16:36:28 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/configs"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-01-30 21:05:20 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/cmd/server/conf"
|
2022-08-16 23:30:23 +08:00
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Init() {
|
2023-02-10 14:22:37 +08:00
|
|
|
baseDir := "/opt"
|
2023-02-13 14:28:04 +08:00
|
|
|
mode := "local"
|
2023-02-10 14:22:37 +08:00
|
|
|
fileOp := files.NewFileOp()
|
2022-08-16 23:30:23 +08:00
|
|
|
v := viper.NewWithOptions()
|
2023-01-29 16:38:34 +08:00
|
|
|
v.SetConfigType("yaml")
|
2023-02-10 14:22:37 +08:00
|
|
|
if fileOp.Stat("/opt/1panel/conf/app.yaml") {
|
|
|
|
v.SetConfigName("app")
|
2023-02-10 18:07:29 +08:00
|
|
|
v.AddConfigPath(path.Join("/opt/1panel/conf"))
|
2023-02-10 14:22:37 +08:00
|
|
|
if err := v.ReadInConfig(); err != nil {
|
|
|
|
panic(fmt.Errorf("Fatal error config file: %s \n", err))
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-13 14:28:04 +08:00
|
|
|
mode = "release"
|
2023-02-10 14:22:37 +08:00
|
|
|
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))
|
|
|
|
}
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|
|
|
|
v.OnConfigChange(func(e fsnotify.Event) {
|
|
|
|
if err := v.Unmarshal(&global.CONF); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
serverConfig := configs.ServerConfig{}
|
|
|
|
if err := v.Unmarshal(&serverConfig); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-02-13 14:28:04 +08:00
|
|
|
if mode == "local" && serverConfig.System.BaseDir != "" {
|
|
|
|
baseDir = serverConfig.System.BaseDir
|
|
|
|
}
|
2022-08-16 23:30:23 +08:00
|
|
|
global.CONF = serverConfig
|
2023-01-29 16:38:34 +08:00
|
|
|
global.CONF.BaseDir = baseDir
|
2023-02-10 09:58:14 +08:00
|
|
|
global.CONF.System.DataDir = global.CONF.BaseDir + "/1panel"
|
|
|
|
global.CONF.System.Cache = global.CONF.System.DataDir + "/cache"
|
|
|
|
global.CONF.System.Backup = global.CONF.System.DataDir + "/backup"
|
|
|
|
global.CONF.System.DbPath = global.CONF.System.DataDir + "/db"
|
|
|
|
global.CONF.System.LogPath = global.CONF.System.DataDir + "/log"
|
2023-01-29 16:38:34 +08:00
|
|
|
global.Viper = v
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|