2024-04-01 09:42:29 +08:00
|
|
|
package config
|
|
|
|
|
2024-04-03 21:53:57 +08:00
|
|
|
import (
|
2024-06-30 21:56:34 +08:00
|
|
|
"fmt"
|
2024-07-01 10:05:42 +08:00
|
|
|
"os"
|
2024-06-30 21:56:34 +08:00
|
|
|
"reflect"
|
2024-06-30 16:12:26 +08:00
|
|
|
"sync"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
2024-04-03 21:53:57 +08:00
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
)
|
2024-04-01 09:42:29 +08:00
|
|
|
|
2024-06-30 16:12:26 +08:00
|
|
|
var (
|
|
|
|
config map[string]interface{}
|
2024-08-17 19:37:24 +08:00
|
|
|
mu sync.RWMutex
|
|
|
|
once sync.Once
|
2024-06-30 16:12:26 +08:00
|
|
|
)
|
2024-04-03 21:53:57 +08:00
|
|
|
|
2024-08-17 19:37:24 +08:00
|
|
|
// loadConfig 加载配置文件
|
|
|
|
func loadConfig() error {
|
|
|
|
_, err := toml.DecodeFile("config.toml", &config)
|
|
|
|
return err
|
2024-06-30 16:12:26 +08:00
|
|
|
}
|
2024-08-17 19:37:24 +08:00
|
|
|
|
|
|
|
// GetConfig 获取配置,使用 sync.Once 确保只加载一次
|
2024-06-30 16:12:26 +08:00
|
|
|
func GetConfig() map[string]interface{} {
|
2024-08-17 19:37:24 +08:00
|
|
|
once.Do(func() {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
if err := loadConfig(); err != nil {
|
|
|
|
fmt.Printf("加载配置文件失败: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
})
|
2024-06-30 16:12:26 +08:00
|
|
|
return config
|
2024-04-01 09:42:29 +08:00
|
|
|
}
|
2024-08-17 19:37:24 +08:00
|
|
|
|
|
|
|
// ReloadConfig 重新加载配置文件
|
|
|
|
func ReloadConfig() error {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
return loadConfig()
|
2024-07-01 10:05:42 +08:00
|
|
|
}
|
2024-08-17 19:37:24 +08:00
|
|
|
|
|
|
|
// ModifyConfig 修改配置并写回文件
|
|
|
|
func ModifyConfig(key string, value interface{}) error {
|
2024-07-01 10:05:42 +08:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
config[key] = value
|
2024-08-17 19:37:24 +08:00
|
|
|
|
2024-07-01 10:05:42 +08:00
|
|
|
file, err := os.Create("config.toml")
|
|
|
|
if err != nil {
|
2024-08-17 19:37:24 +08:00
|
|
|
return err
|
2024-07-01 10:05:42 +08:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
encoder := toml.NewEncoder(file)
|
2024-08-17 19:37:24 +08:00
|
|
|
return encoder.Encode(config)
|
2024-07-01 10:05:42 +08:00
|
|
|
}
|
2024-06-30 21:56:34 +08:00
|
|
|
|
2024-08-17 19:37:24 +08:00
|
|
|
// PrintConfig 递归打印配置内容
|
2024-06-30 21:56:34 +08:00
|
|
|
func PrintConfig(m map[string]interface{}, indent string) {
|
|
|
|
for key, value := range m {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
fmt.Printf("%s%s (type: %s):\n", indent, key, reflect.TypeOf(v))
|
|
|
|
PrintConfig(v, indent+" ")
|
|
|
|
case []interface{}:
|
|
|
|
fmt.Printf("%s%s:\n", indent, key)
|
|
|
|
for i, item := range v {
|
|
|
|
switch itemValue := item.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
fmt.Printf("%s [%d] (type: %s):\n", indent, i, reflect.TypeOf(itemValue))
|
|
|
|
PrintConfig(itemValue, indent+" ")
|
|
|
|
default:
|
|
|
|
fmt.Printf("%s [%d] (type: %s): %v\n", indent, i, reflect.TypeOf(itemValue), item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Printf("%s%s (type: %s): %v\n", indent, key, reflect.TypeOf(value), value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|