2022-08-16 23:30:23 +08:00
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/configs"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-10-12 18:57:22 +08:00
|
|
|
"path"
|
2022-08-16 23:30:23 +08:00
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Init() {
|
|
|
|
v := viper.NewWithOptions()
|
|
|
|
v.SetConfigName("app")
|
|
|
|
v.SetConfigType("yml")
|
|
|
|
v.AddConfigPath("/opt/1Panel/conf")
|
|
|
|
if err := v.ReadInConfig(); err != nil {
|
|
|
|
panic(fmt.Errorf("Fatal error config file: %s \n", err))
|
|
|
|
}
|
|
|
|
v.WatchConfig()
|
|
|
|
v.OnConfigChange(func(e fsnotify.Event) {
|
|
|
|
fmt.Println("config file changed:", e.Name)
|
|
|
|
if err := v.Unmarshal(&global.CONF); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
serverConfig := configs.ServerConfig{}
|
|
|
|
if err := v.Unmarshal(&serverConfig); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
global.CONF = serverConfig
|
2022-10-12 18:57:22 +08:00
|
|
|
InitDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitDir() {
|
|
|
|
constant.DefaultDataDir = "/opt/1Panel/data"
|
|
|
|
constant.ResourceDir = path.Join(constant.DefaultDataDir, "resource")
|
|
|
|
constant.AppResourceDir = path.Join(constant.ResourceDir, "apps")
|
|
|
|
constant.AppInstallDir = path.Join(constant.DefaultDataDir, "apps")
|
|
|
|
constant.BackupDir = path.Join(constant.DefaultDataDir, "backup")
|
|
|
|
constant.AppBackupDir = path.Join(constant.BackupDir, "apps")
|
|
|
|
|
|
|
|
//TODO 创建文件夹
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|