package config import ( "fmt" "gopkg.in/yaml.v3" "os" "time" ) var Config = &AllConfig{} // AllConfig 应用程序配置结构体 type AllConfig struct { Server ServerConfig `yaml:"server"` Etcd EtcdConfig `yaml:"etcd"` Logger LoggerConfig `yaml:"logger"` Database DatabaseConfig `yaml:"database"` Redis RedisConfig `yaml:"redis"` Kafka KafkaConfig `yaml:"kafka"` Alert AlertConfig `yaml:"alert"` Upload Oss `yaml:"upload"` //上传配置 } // ServerConfig defines the configuration for the server. type ServerConfig struct { Env string `yaml:"env"` // 动环境,取值为 "dev"、"test" 或 "prod",默认为 "dev" LinkDomain string `yaml:"link_domain"` //链接的域名 LinkDomainProto string `yaml:"link_domain_proto"` //链接的域名协议 LadderHost string `yaml:"ladder_host"` //转发地址 AdsDomain string `yaml:"ads_domain"` //广告活动的域名 AdsDomainProto string `yaml:"ads_domain_proto"` //广告活动的域名协议 AdsJump string `yaml:"ads_jump"` //广告策略调整地址 } // EtcdConfig defines the configuration for etcd type EtcdConfig struct { Address []string `yaml:"address"` } // LoggerConfig defines the configuration for logging. type LoggerConfig struct { StdOut bool `yaml:"std_out"` FileOut bool `yaml:"file_out"` Path string `yaml:"path"` MaxSize int `yaml:"max_size"` MaxBackups int `yaml:"max_backups"` MaxAge int `yaml:"max_age"` Compress bool `yaml:"compress"` } // DatabaseConfig defines the configuration for the database. type DatabaseConfig struct { URL string `yaml:"url"` // 数据库连接地址 MaxIdleConn int `yaml:"max_idle_conn"` // 最大空闲连接数,默认为10 MaxOpenConn int `yaml:"max_open_conn"` // 最大活动连接数,默认为100 ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime"` // 连接最大生命周期(单位:秒) 默认为3600 SlowThreshold time.Duration `yaml:"slow_threshold"` //慢sql警告阈值(单位:毫秒) } // RedisConfig defines the configuration for Redis. type RedisConfig struct { Addr string `yaml:"addr"` // Redis连接地址 Password string `yaml:"password"` // Redis连接密码 DB int `yaml:"db"` // 连接库 Sentinel string `yaml:"sentinel"` // 哨兵模式时 哨兵地址,没有则不填 SentinelMasterName string `yaml:"sentinel_master_name"` // 哨兵模式下master名称,没有则不填 } // KafkaConfig defines the configuration for Kafka. type KafkaConfig struct { Brokers []string `yaml:"brokers"` // Kafka连接地址,格式为 "host1:port1,host2:port2,...",也支持多行格式 } // AlertConfig defines the configuration for alerting. type AlertConfig struct { FeiShu FeiShuConfig `yaml:"feishu"` // 飞书告警配置 Scenes []FeiShuScenesConfig `yaml:"scenes"` // 场景配置 } type FeiShuConfig struct { Url string `yaml:"url"` // 告警地址 Secret string `yaml:"secret"` // 告警地址密码 } type FeiShuScenesConfig struct { Url string `yaml:"url"` // 告警地址 Secret string `yaml:"secret"` // 告警地址密码 Scene string `yaml:"scene"` //场景 } // FilePath 配置文件路径信息 type FilePath struct { ConfigName string // 设置配置文件名 (不需要后缀) ConfigType string // 设置配置文件类型 ConfigPath string // 设置配置文件路径 } type Oss struct { AccessKey string `yaml:"access_key"` //AccessKeyId Secret string `yaml:"secret"` //AccessKeySecret UploadUrl string `yaml:"upload_url"` //阿里云上传地址 Endpoint string `yaml:"endpoint"` //访问 OSS 服务的时候使用的域名 BucketName string `yaml:"bucket_name"` //存储空间名称 Prefix string `yaml:"prefix"` //上传文件路径前缀 Host string `yaml:"host"` //全球访问host } // LoadConfig 从文件中加载配置 func LoadConfig(filepath FilePath) error { gByte, err := os.ReadFile(fmt.Sprintf("%v/%v.yaml", filepath.ConfigPath, filepath.ConfigName)) if err != nil { panic(err) } err = yaml.Unmarshal(gByte, &Config) // 将读取的配置映射到 Config 结构体中 if err != nil { return fmt.Errorf("failed to unmarshal config file: %s", err) } return nil }