You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.1 KiB

1 month ago
package config
import (
"fmt"
1 month ago
"github.com/spf13/viper"
)
type Config struct {
DB DBConfig
Server ServerConfig
Redis RedisConfig
JWT JWTConfig
Log LogConfig // 新增日志配置
1 month ago
}
type DBConfig struct {
Host string
Port string
User string
Password string
Name string
}
type ServerConfig struct {
Port string
}
type RedisConfig struct {
Host string
Port string
Password string
DB int
}
type JWTConfig struct {
Secret string
Expire int // 小时
}
// 新增日志配置结构
type LogConfig struct {
Level string // 日志级别: debug, info, warn, error
Path string // 日志文件路径
}
func LoadConfig() (*Config, error) {
1 month ago
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("error reading config file: %w", err)
1 month ago
}
var cfg Config
if err := viper.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("unable to decode into struct: %w", err)
1 month ago
}
fmt.Println("Configuration loaded successfully")
return &cfg, nil
1 month ago
}