版本封存
This commit is contained in:
@@ -1,24 +1,42 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"cmii-uav-watchdog-common/wdd_log"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
localAuthFilePath = "/cmii/cmii-uav-watchdog/auth_code.json"
|
||||
configFilePath = "/cmii/cmii-uav-watchdog/config.yaml"
|
||||
)
|
||||
|
||||
// Config 配置结构体
|
||||
type Config struct {
|
||||
Server struct {
|
||||
Port string `mapstructure:"port"`
|
||||
Port string `mapstructure:"port"`
|
||||
Debug bool `mapstructure:"debug"`
|
||||
} `mapstructure:"server"`
|
||||
|
||||
Auth struct {
|
||||
Secret string `mapstructure:"secret"` // TOTP密钥
|
||||
AuthFilePath string `mapstructure:"auth_file"` // 授权文件路径
|
||||
TimeOffsetAllowed int64 `mapstructure:"time_offset_allowed"` // 允许的时间偏移(秒)
|
||||
} `mapstructure:"auth"`
|
||||
TierOneAuth struct {
|
||||
TierOneSecret string `mapstructure:"tier_one_secret" yaml:"tier_one_secret"` // TOTP密钥
|
||||
TimeOffsetAllowed int64 `mapstructure:"time_offset_allowed" yaml:"time_offset_allowed"` // 允许的时间偏移(秒)
|
||||
LocalAuthFilePath string `mapstructure:"local_auth_file_path" yaml:"local_auth_file_path"` // 本地授权文件路径
|
||||
} `mapstructure:"tier_one_auth" yaml:"tier_one_auth"`
|
||||
|
||||
WatchdogCenter struct {
|
||||
URL string `mapstructure:"url"` // 一级授权中心地址
|
||||
} `mapstructure:"watchdog_center"`
|
||||
URL string `mapstructure:"url" yaml:"url"` // 一级授权中心地址
|
||||
} `mapstructure:"watchdog_center" yaml:"watchdog_center"`
|
||||
|
||||
Project struct {
|
||||
ProjectNamespace string `mapstructure:"project_namespace" yaml:"project_namespace"` // 项目命名空间
|
||||
} `mapstructure:"project" yaml:"project"`
|
||||
|
||||
TierTwoAuth struct {
|
||||
TierTwoSecret string `mapstructure:"tier_two_secret" yaml:"tier_two_secret"` // 二级授权密钥
|
||||
} `mapstructure:"tier_two_auth" yaml:"tier_two_auth"`
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -27,22 +45,56 @@ var (
|
||||
)
|
||||
|
||||
// LoadConfig 加载配置
|
||||
func LoadConfig(path string) error {
|
||||
once.Do(func() {
|
||||
// 设置默认配置值
|
||||
config.Server.Port = "8080"
|
||||
config.Auth.Secret = "default-secret-key-please-change-in-production"
|
||||
config.Auth.AuthFilePath = "./auth-storage.json"
|
||||
config.Auth.TimeOffsetAllowed = 3600 // 默认允许1小时的时间偏移
|
||||
config.WatchdogCenter.URL = "http://localhost:8081"
|
||||
// 单例模式, 如果已经初始化过, 则直接返回
|
||||
// 如果config文件不存在,报错 无法启动
|
||||
// 不使用viper,用最简单的方式读取配置文件,解析其中的配置
|
||||
// 如果解析失败,无法得到config 则报错 无法启动
|
||||
func LoadConfig() error {
|
||||
|
||||
var err error
|
||||
once.Do(func() {
|
||||
// 读取配置文件
|
||||
data, readErr := os.ReadFile(configFilePath)
|
||||
if readErr != nil {
|
||||
wdd_log.Fatal("无法读取配置文件: %v", readErr)
|
||||
err = readErr
|
||||
return
|
||||
}
|
||||
|
||||
// 解析配置文件
|
||||
parseErr := yaml.Unmarshal(data, &config)
|
||||
if parseErr != nil {
|
||||
wdd_log.Fatal("无法解析配置文件: %v", parseErr)
|
||||
err = parseErr
|
||||
return
|
||||
}
|
||||
|
||||
// 设置本地授权文件保存地点
|
||||
config.TierOneAuth.LocalAuthFilePath = localAuthFilePath
|
||||
wdd_log.Info("[Config] - 本地授权文件保存地点: %s", config.TierOneAuth.LocalAuthFilePath)
|
||||
|
||||
// 初始化日志系统
|
||||
InitLogger()
|
||||
|
||||
// 这里可以添加从文件加载配置的逻辑,比如使用标准库读取JSON或YAML文件
|
||||
// 但目前使用默认值替代
|
||||
})
|
||||
return nil
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// GetConfig 获取配置
|
||||
func GetConfig() *Config {
|
||||
return &config
|
||||
}
|
||||
|
||||
// InitLogger 初始化日志系统
|
||||
// 从环境变量CMII_UAV_WATCHDOG_DEBUG读取配置,决定是否开启debug日志
|
||||
func InitLogger() {
|
||||
// 获取Logger单例实例
|
||||
logger := wdd_log.GetInstance()
|
||||
|
||||
// 设置是否启用Debug日志
|
||||
logger.SetEnableDebug(config.Server.Debug)
|
||||
|
||||
// 记录日志初始化信息
|
||||
logger.Info("日志系统初始化完成,Debug日志状态: %v", config.Server.Debug)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user