2022-08-16 15:30:23 +00:00
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
2023-01-30 13:05:20 +00:00
|
|
|
"bytes"
|
2022-08-16 15:30:23 +00:00
|
|
|
"fmt"
|
2023-02-10 06:22:37 +00:00
|
|
|
"path"
|
2023-02-01 08:46:13 +00:00
|
|
|
"strings"
|
2023-01-06 10:53:25 +00:00
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/configs"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-02-14 03:28:38 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2023-01-30 13:05:20 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/cmd/server/conf"
|
2022-08-16 15:30:23 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
"github.com/spf13/viper"
|
2023-02-14 03:28:38 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-08-16 15:30:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Init() {
|
2023-02-10 06:22:37 +00:00
|
|
|
baseDir := "/opt"
|
2023-03-10 05:47:31 +00:00
|
|
|
port := "9999"
|
2023-02-21 07:19:12 +00:00
|
|
|
mode := ""
|
2023-04-02 08:54:00 +00:00
|
|
|
version := "v1.0.0"
|
2023-04-27 14:44:16 +00:00
|
|
|
username, password, entrance := "", "", ""
|
2023-02-10 06:22:37 +00:00
|
|
|
fileOp := files.NewFileOp()
|
2022-08-16 15:30:23 +00:00
|
|
|
v := viper.NewWithOptions()
|
2023-01-29 08:38:34 +00:00
|
|
|
v.SetConfigType("yaml")
|
2023-02-14 03:17:18 +00:00
|
|
|
|
|
|
|
config := configs.ServerConfig{}
|
|
|
|
if err := yaml.Unmarshal(conf.AppYaml, &config); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if config.System.Mode != "" {
|
|
|
|
mode = config.System.Mode
|
|
|
|
}
|
2023-02-17 07:13:55 +00:00
|
|
|
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") {
|
2023-02-10 06:22:37 +00:00
|
|
|
v.SetConfigName("app")
|
2023-02-10 10:07:29 +00:00
|
|
|
v.AddConfigPath(path.Join("/opt/1panel/conf"))
|
2023-02-10 06:22:37 +00:00
|
|
|
if err := v.ReadInConfig(); err != nil {
|
|
|
|
panic(fmt.Errorf("Fatal error config file: %s \n", err))
|
|
|
|
}
|
|
|
|
} else {
|
2023-03-14 09:14:57 +00:00
|
|
|
baseDir = loadParams("BASE_DIR")
|
2023-04-27 14:44:16 +00:00
|
|
|
port = loadParams("ORIGINAL_PORT")
|
|
|
|
version = loadParams("ORIGINAL_VERSION")
|
|
|
|
username = loadParams("ORIGINAL_USERNAME")
|
|
|
|
password = loadParams("ORIGINAL_PASSWORD")
|
|
|
|
entrance = loadParams("ORIGINAL_ENTRANCE")
|
2023-03-10 05:47:31 +00:00
|
|
|
|
2023-02-10 06:22:37 +00: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 15:30:23 +00: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-04-27 14:44:16 +00:00
|
|
|
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") {
|
|
|
|
if serverConfig.System.BaseDir != "" {
|
|
|
|
baseDir = serverConfig.System.BaseDir
|
|
|
|
}
|
|
|
|
if serverConfig.System.Port != "" {
|
|
|
|
port = serverConfig.System.Port
|
|
|
|
}
|
|
|
|
if serverConfig.System.Version != "" {
|
|
|
|
version = serverConfig.System.Version
|
|
|
|
}
|
|
|
|
if serverConfig.System.Username != "" {
|
2023-05-09 09:31:44 +00:00
|
|
|
username = serverConfig.System.Username
|
2023-04-27 14:44:16 +00:00
|
|
|
}
|
|
|
|
if serverConfig.System.Password != "" {
|
2023-05-09 09:31:44 +00:00
|
|
|
password = serverConfig.System.Password
|
2023-04-27 14:44:16 +00:00
|
|
|
}
|
|
|
|
if serverConfig.System.Entrance != "" {
|
2023-05-09 09:31:44 +00:00
|
|
|
entrance = serverConfig.System.Entrance
|
2023-04-27 14:44:16 +00:00
|
|
|
}
|
2023-03-14 09:14:57 +00:00
|
|
|
}
|
2023-02-17 09:19:35 +00:00
|
|
|
|
2022-08-16 15:30:23 +00:00
|
|
|
global.CONF = serverConfig
|
2023-03-14 13:55:28 +00:00
|
|
|
global.CONF.System.BaseDir = baseDir
|
2023-02-21 07:19:12 +00:00
|
|
|
global.CONF.System.IsDemo = v.GetBool("system.is_demo")
|
2023-07-27 07:52:25 +00:00
|
|
|
global.CONF.System.DataDir = path.Join(global.CONF.System.BaseDir, "1panel")
|
|
|
|
global.CONF.System.Cache = path.Join(global.CONF.System.DataDir, "cache")
|
|
|
|
global.CONF.System.Backup = path.Join(global.CONF.System.DataDir, "backup")
|
|
|
|
global.CONF.System.DbPath = path.Join(global.CONF.System.DataDir, "db")
|
|
|
|
global.CONF.System.LogPath = path.Join(global.CONF.System.DataDir, "log")
|
|
|
|
global.CONF.System.TmpDir = path.Join(global.CONF.System.DataDir, "tmp")
|
2023-03-10 05:47:31 +00:00
|
|
|
global.CONF.System.Port = port
|
2023-03-14 09:14:57 +00:00
|
|
|
global.CONF.System.Version = version
|
2023-04-27 14:44:16 +00:00
|
|
|
global.CONF.System.Username = username
|
|
|
|
global.CONF.System.Password = password
|
|
|
|
global.CONF.System.Entrance = entrance
|
2024-04-16 06:44:07 +00:00
|
|
|
global.CONF.System.ChangeUserInfo = loadChangeInfo()
|
2023-01-29 08:38:34 +00:00
|
|
|
global.Viper = v
|
2022-08-16 15:30:23 +00:00
|
|
|
}
|
2023-03-14 09:14:57 +00:00
|
|
|
|
|
|
|
func loadParams(param string) string {
|
2024-05-23 02:14:49 +00:00
|
|
|
stdout, err := cmd.Execf("grep '^%s=' /usr/local/bin/1pctl | cut -d'=' -f2", param)
|
2023-03-14 09:14:57 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-05-04 06:14:38 +00:00
|
|
|
info := strings.ReplaceAll(stdout, "\n", "")
|
|
|
|
if len(info) == 0 || info == `""` {
|
2024-05-23 02:14:49 +00:00
|
|
|
panic(fmt.Sprintf("error `%s` find in /usr/local/bin/1pctl", param))
|
2023-03-14 09:14:57 +00:00
|
|
|
}
|
2023-05-04 06:14:38 +00:00
|
|
|
return info
|
2023-03-14 09:14:57 +00:00
|
|
|
}
|
2023-05-13 04:20:29 +00:00
|
|
|
|
2024-04-16 06:44:07 +00:00
|
|
|
func loadChangeInfo() string {
|
2024-05-23 02:14:49 +00:00
|
|
|
stdout, err := cmd.Exec("grep '^CHANGE_USER_INFO=' /usr/local/bin/1pctl | cut -d'=' -f2")
|
2023-05-13 04:20:29 +00:00
|
|
|
if err != nil {
|
2024-04-16 06:44:07 +00:00
|
|
|
return ""
|
2023-05-13 04:20:29 +00:00
|
|
|
}
|
2024-04-16 06:44:07 +00:00
|
|
|
return strings.ReplaceAll(stdout, "\n", "")
|
2023-05-13 04:20:29 +00:00
|
|
|
}
|