Files
cmii-uav-watchdog-project/cmii-uav-watchdog/config/config.go

49 lines
1.2 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 (
"sync"
)
// Config 配置结构体
type Config struct {
Server struct {
Port string `mapstructure:"port"`
} `mapstructure:"server"`
Auth struct {
Secret string `mapstructure:"secret"` // TOTP密钥
AuthFilePath string `mapstructure:"auth_file"` // 授权文件路径
TimeOffsetAllowed int64 `mapstructure:"time_offset_allowed"` // 允许的时间偏移(秒)
} `mapstructure:"auth"`
WatchdogCenter struct {
URL string `mapstructure:"url"` // 一级授权中心地址
} `mapstructure:"watchdog_center"`
}
var (
config Config
once sync.Once
)
// 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"
// 这里可以添加从文件加载配置的逻辑比如使用标准库读取JSON或YAML文件
// 但目前使用默认值替代
})
return nil
}
// GetConfig 获取配置
func GetConfig() *Config {
return &config
}