1Panel/backend/init/viper/viper.go

98 lines
2.9 KiB
Go
Raw Normal View History

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
"path"
2023-02-01 16:46:13 +08:00
"strings"
2023-01-06 18:53:25 +08:00
"github.com/1Panel-dev/1Panel/backend/configs"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/files"
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"
"gopkg.in/yaml.v3"
2022-08-16 23:30:23 +08:00
)
func Init() {
2023-02-10 14:22:37 +08:00
baseDir := "/opt"
port := "9999"
2023-02-21 15:19:12 +08:00
mode := ""
2023-04-02 16:54:00 +08:00
version := "v1.0.0"
2023-02-10 14:22:37 +08:00
fileOp := files.NewFileOp()
2022-08-16 23:30:23 +08:00
v := viper.NewWithOptions()
v.SetConfigType("yaml")
2023-02-14 11:17:18 +08:00
config := configs.ServerConfig{}
if err := yaml.Unmarshal(conf.AppYaml, &config); err != nil {
panic(err)
}
if config.System.Mode != "" {
mode = config.System.Mode
}
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") {
2023-02-10 14:22:37 +08:00
v.SetConfigName("app")
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 {
baseDir = loadParams("BASE_DIR")
port = loadParams("PANEL_PORT")
version = loadParams("ORIGINAL_INSTALLED_VERSION")
2023-02-20 15:39:26 +08:00
if strings.HasSuffix(baseDir, "/") {
baseDir = baseDir[:strings.LastIndex(baseDir, "/")]
}
2023-02-10 14:22:37 +08:00
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)
}
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") && serverConfig.System.BaseDir != "" {
baseDir = serverConfig.System.BaseDir
}
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") && serverConfig.System.Port != "" {
port = serverConfig.System.Port
}
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") && serverConfig.System.Version != "" {
version = serverConfig.System.Version
}
2022-08-16 23:30:23 +08:00
global.CONF = serverConfig
global.CONF.System.BaseDir = baseDir
2023-02-21 15:19:12 +08:00
global.CONF.System.IsDemo = v.GetBool("system.is_demo")
global.CONF.System.DataDir = global.CONF.System.BaseDir + "/1panel"
2023-02-10 09:58:14 +08:00
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"
global.CONF.System.TmpDir = global.CONF.System.DataDir + "/tmp"
global.CONF.System.Port = port
global.CONF.System.Version = version
global.Viper = v
2022-08-16 23:30:23 +08:00
}
func loadParams(param string) string {
stdout, err := cmd.Execf("grep '^%s=' /usr/bin/1pctl | cut -d'=' -f2", param)
if err != nil {
panic(err)
}
baseDir := strings.ReplaceAll(stdout, "\n", "")
if len(baseDir) == 0 {
panic(fmt.Sprintf("error `%s` find in /usr/bin/1pctl", param))
}
return baseDir
}