Files
cmii-uav-watchdog-project/cmii-uav-watchdog/config/config.go
2025-12-06 11:26:05 +08:00

101 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"`
Debug bool `mapstructure:"debug"`
} `mapstructure:"server"`
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" 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 (
config Config
once sync.Once
)
// LoadConfig 加载配置
// 单例模式, 如果已经初始化过, 则直接返回
// 如果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()
})
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)
}