Files
EdgeAPI/internal/configs/api_config.go

135 lines
2.8 KiB
Go
Raw Normal View History

2020-07-21 17:27:00 +08:00
package configs
import (
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
2020-07-21 17:27:00 +08:00
"github.com/iwind/TeaGo/Tea"
2022-03-04 12:31:44 +08:00
"gopkg.in/yaml.v3"
"os"
"path/filepath"
2020-07-21 17:27:00 +08:00
)
var sharedAPIConfig *APIConfig = nil
2021-11-20 18:59:35 +08:00
// APIConfig API节点配置
2020-07-21 17:27:00 +08:00
type APIConfig struct {
2020-10-04 14:27:14 +08:00
NodeId string `yaml:"nodeId" json:"nodeId"`
Secret string `yaml:"secret" json:"secret"`
numberId int64 // 数字ID
2020-07-21 17:27:00 +08:00
}
2021-11-20 18:59:35 +08:00
// SharedAPIConfig 获取共享配置
2020-07-21 17:27:00 +08:00
func SharedAPIConfig() (*APIConfig, error) {
sharedLocker.Lock()
defer sharedLocker.Unlock()
if sharedAPIConfig != nil {
return sharedAPIConfig, nil
}
// 候选文件
localFile := Tea.ConfigFile("api.yaml")
isFromLocal := false
paths := []string{localFile}
2021-02-01 09:26:20 +08:00
homeDir, homeErr := os.UserHomeDir()
if homeErr == nil {
paths = append(paths, homeDir+"/."+teaconst.ProcessName+"/api.yaml")
}
paths = append(paths, "/etc/"+teaconst.ProcessName+"/api.yaml")
// 依次检查文件
var data []byte
2021-02-01 09:26:20 +08:00
var err error
for _, path := range paths {
2022-08-04 11:41:42 +08:00
data, err = os.ReadFile(path)
if err == nil {
if path == localFile {
isFromLocal = true
}
break
}
}
2020-07-21 17:27:00 +08:00
if err != nil {
return nil, err
}
// 解析内容
2020-07-21 17:27:00 +08:00
config := &APIConfig{}
err = yaml.Unmarshal(data, config)
if err != nil {
return nil, err
}
if !isFromLocal {
// 恢复文件
2022-08-04 11:41:42 +08:00
_ = os.WriteFile(localFile, data, 0666)
}
// 恢复数据库文件
{
dbConfigFile := Tea.ConfigFile("db.yaml")
_, err := os.Stat(dbConfigFile)
2021-11-01 15:31:59 +08:00
if err != nil {
paths := []string{}
2021-02-01 09:26:20 +08:00
homeDir, homeErr := os.UserHomeDir()
if homeErr == nil {
paths = append(paths, homeDir+"/."+teaconst.ProcessName+"/db.yaml")
}
paths = append(paths, "/etc/"+teaconst.ProcessName+"/db.yaml")
for _, path := range paths {
_, err := os.Stat(path)
if err == nil {
2022-08-04 11:41:42 +08:00
data, err := os.ReadFile(path)
if err == nil {
2022-08-04 11:41:42 +08:00
_ = os.WriteFile(dbConfigFile, data, 0666)
break
}
}
}
}
}
2020-07-21 17:27:00 +08:00
sharedAPIConfig = config
return config, nil
}
2020-10-04 14:27:14 +08:00
2021-11-20 18:59:35 +08:00
// SetNumberId 设置数字ID
2020-10-04 14:27:14 +08:00
func (this *APIConfig) SetNumberId(numberId int64) {
this.numberId = numberId
2021-11-20 18:59:35 +08:00
teaconst.NodeId = numberId
2020-10-04 14:27:14 +08:00
}
2021-11-20 18:59:35 +08:00
// NumberId 获取数字ID
2020-10-04 14:27:14 +08:00
func (this *APIConfig) NumberId() int64 {
return this.numberId
}
2020-10-13 20:05:13 +08:00
2021-11-20 18:59:35 +08:00
// WriteFile 保存到文件
2020-10-13 20:05:13 +08:00
func (this *APIConfig) WriteFile(path string) error {
data, err := yaml.Marshal(this)
if err != nil {
return err
}
// 生成备份文件
filename := filepath.Base(path)
homeDir, _ := os.UserHomeDir()
backupDirs := []string{"/etc/edge-api"}
if len(homeDir) > 0 {
backupDirs = append(backupDirs, homeDir+"/.edge-api")
}
for _, backupDir := range backupDirs {
stat, err := os.Stat(backupDir)
if err == nil && stat.IsDir() {
2022-08-04 11:41:42 +08:00
_ = os.WriteFile(backupDir+"/"+filename, data, 0666)
} else if err != nil && os.IsNotExist(err) {
err = os.Mkdir(backupDir, 0777)
if err == nil {
2022-08-04 11:41:42 +08:00
_ = os.WriteFile(backupDir+"/"+filename, data, 0666)
}
}
}
2022-08-04 11:41:42 +08:00
return os.WriteFile(path, data, 0666)
2020-10-13 20:05:13 +08:00
}