2022-06-29 11:09:58 +08:00
|
|
|
/*
|
|
|
|
* @Author: LinkLeong link@icewhale.com
|
|
|
|
* @Date: 2022-05-13 18:15:46
|
|
|
|
* @LastEditors: LinkLeong
|
2022-09-06 14:28:49 +08:00
|
|
|
* @LastEditTime: 2022-09-05 11:58:02
|
2022-06-29 11:09:58 +08:00
|
|
|
* @FilePath: /CasaOS/pkg/config/init.go
|
|
|
|
* @Description:
|
|
|
|
* @Website: https://www.casaos.io
|
|
|
|
* Copyright (c) 2022 by icewhale, All Rights Reserved.
|
|
|
|
*/
|
2021-09-26 10:35:02 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2021-10-22 16:49:09 +08:00
|
|
|
|
|
|
|
"github.com/IceWhaleTech/CasaOS/model"
|
|
|
|
"github.com/go-ini/ini"
|
2021-09-26 10:35:02 +08:00
|
|
|
)
|
|
|
|
|
2022-10-20 02:25:26 -04:00
|
|
|
// 系统配置
|
2021-09-26 10:35:02 +08:00
|
|
|
var SysInfo = &model.SysInfoModel{}
|
|
|
|
|
2022-10-20 02:25:26 -04:00
|
|
|
// 用户相关
|
2021-09-26 10:35:02 +08:00
|
|
|
var AppInfo = &model.APPModel{}
|
|
|
|
|
2022-09-06 14:28:49 +08:00
|
|
|
var CommonInfo = &model.CommonModel{}
|
|
|
|
|
2022-11-29 12:17:14 -05:00
|
|
|
// var RedisInfo = &model.RedisModel{}
|
2021-09-26 10:35:02 +08:00
|
|
|
|
2022-10-20 02:25:26 -04:00
|
|
|
// server相关
|
2021-09-26 10:35:02 +08:00
|
|
|
var ServerInfo = &model.ServerModel{}
|
|
|
|
|
|
|
|
var SystemConfigInfo = &model.SystemConfig{}
|
|
|
|
|
2022-03-16 15:41:14 +08:00
|
|
|
var FileSettingInfo = &model.FileSetting{}
|
|
|
|
|
2021-09-26 10:35:02 +08:00
|
|
|
var Cfg *ini.File
|
|
|
|
|
2022-10-20 02:25:26 -04:00
|
|
|
// 初始化设置,获取系统的部分信息。
|
2021-09-26 10:35:02 +08:00
|
|
|
func InitSetup(config string) {
|
2022-11-29 12:17:14 -05:00
|
|
|
configDir := USERCONFIGURL
|
2021-09-26 10:35:02 +08:00
|
|
|
if len(config) > 0 {
|
|
|
|
configDir = config
|
|
|
|
}
|
2022-06-29 11:09:58 +08:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
configDir = "./conf/conf.conf"
|
|
|
|
}
|
2021-09-26 10:35:02 +08:00
|
|
|
var err error
|
2022-11-29 12:17:14 -05:00
|
|
|
// 读取文件
|
2021-09-26 10:35:02 +08:00
|
|
|
Cfg, err = ini.Load(configDir)
|
|
|
|
if err != nil {
|
2022-09-06 14:28:49 +08:00
|
|
|
Cfg, err = ini.Load("/etc/casaos.conf")
|
|
|
|
if err != nil {
|
|
|
|
Cfg, err = ini.Load("/casaOS/server/conf/conf.ini")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Fail to read file: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 10:35:02 +08:00
|
|
|
}
|
|
|
|
mapTo("app", AppInfo)
|
2022-11-29 12:17:14 -05:00
|
|
|
// mapTo("redis", RedisInfo)
|
2021-09-26 10:35:02 +08:00
|
|
|
mapTo("server", ServerInfo)
|
|
|
|
mapTo("system", SystemConfigInfo)
|
2022-03-16 15:41:14 +08:00
|
|
|
mapTo("file", FileSettingInfo)
|
2022-09-06 14:28:49 +08:00
|
|
|
mapTo("common", CommonInfo)
|
2021-10-22 16:49:09 +08:00
|
|
|
SystemConfigInfo.ConfigPath = configDir
|
2022-06-29 11:09:58 +08:00
|
|
|
if len(AppInfo.DBPath) == 0 {
|
|
|
|
AppInfo.DBPath = "/var/lib/casaos"
|
|
|
|
}
|
|
|
|
if len(AppInfo.LogPath) == 0 {
|
|
|
|
AppInfo.LogPath = "/var/log/casaos/"
|
|
|
|
}
|
|
|
|
if len(AppInfo.ShellPath) == 0 {
|
|
|
|
AppInfo.ShellPath = "/usr/share/casaos/shell"
|
|
|
|
}
|
|
|
|
if len(AppInfo.UserDataPath) == 0 {
|
|
|
|
AppInfo.UserDataPath = "/var/lib/casaos/conf"
|
|
|
|
}
|
2022-09-06 14:28:49 +08:00
|
|
|
if len(CommonInfo.RuntimePath) == 0 {
|
|
|
|
CommonInfo.RuntimePath = "/var/run/casaos"
|
|
|
|
}
|
|
|
|
Cfg.SaveTo(configDir)
|
2021-09-27 14:17:36 +08:00
|
|
|
// AppInfo.ProjectPath = getCurrentDirectory() //os.Getwd()
|
2021-09-26 10:35:02 +08:00
|
|
|
}
|
|
|
|
|
2022-10-20 02:25:26 -04:00
|
|
|
// 映射
|
2021-09-26 10:35:02 +08:00
|
|
|
func mapTo(section string, v interface{}) {
|
|
|
|
err := Cfg.Section(section).MapTo(v)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Cfg.MapTo %s err: %v", section, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取当前执行文件绝对路径(go run)
|
|
|
|
func getCurrentAbPathByCaller() string {
|
|
|
|
var abPath string
|
|
|
|
_, filename, _, ok := runtime.Caller(0)
|
|
|
|
if ok {
|
|
|
|
abPath = path.Dir(filename)
|
|
|
|
}
|
|
|
|
return abPath
|
|
|
|
}
|
2022-11-29 12:17:14 -05:00
|
|
|
|
2021-09-26 10:35:02 +08:00
|
|
|
func getCurrentDirectory() string {
|
|
|
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return strings.Replace(dir, "\\", "/", -1)
|
|
|
|
}
|