mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 23:20:26 +08:00
启动的时候尝试从备份文件中恢复配置
This commit is contained in:
@@ -2,9 +2,12 @@ package configs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||
"github.com/go-yaml/yaml"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var sharedAPIConfig *APIConfig = nil
|
||||
@@ -27,17 +30,67 @@ func SharedAPIConfig() (*APIConfig, error) {
|
||||
return sharedAPIConfig, nil
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadFile(Tea.ConfigFile("api.yaml"))
|
||||
// 候选文件
|
||||
localFile := Tea.ConfigFile("api.yaml")
|
||||
isFromLocal := false
|
||||
paths := []string{localFile}
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
paths = append(paths, homeDir+"/."+teaconst.ProcessName+"/api.yaml")
|
||||
}
|
||||
paths = append(paths, "/etc/"+teaconst.ProcessName+"/api.yaml")
|
||||
|
||||
// 依次检查文件
|
||||
var data []byte
|
||||
for _, path := range paths {
|
||||
data, err = ioutil.ReadFile(path)
|
||||
if err == nil {
|
||||
if path == localFile {
|
||||
isFromLocal = true
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 解析内容
|
||||
config := &APIConfig{}
|
||||
err = yaml.Unmarshal(data, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !isFromLocal {
|
||||
// 恢复文件
|
||||
_ = ioutil.WriteFile(localFile, data, 0666)
|
||||
}
|
||||
|
||||
// 恢复数据库文件
|
||||
{
|
||||
dbConfigFile := Tea.ConfigFile("db.yaml")
|
||||
_, err := os.Stat(dbConfigFile)
|
||||
if err == nil {
|
||||
paths := []string{}
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err == 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 {
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err == nil {
|
||||
_ = ioutil.WriteFile(dbConfigFile, data, 0666)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sharedAPIConfig = config
|
||||
return config, nil
|
||||
}
|
||||
@@ -59,5 +112,25 @@ func (this *APIConfig) WriteFile(path string) error {
|
||||
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() {
|
||||
_ = ioutil.WriteFile(backupDir+"/"+filename, data, 0666)
|
||||
} else if err != nil && os.IsNotExist(err) {
|
||||
err = os.Mkdir(backupDir, 0777)
|
||||
if err == nil {
|
||||
_ = ioutil.WriteFile(backupDir+"/"+filename, data, 0666)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(path, data, 0666)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user