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.
63 lines
1.0 KiB
63 lines
1.0 KiB
package config |
|
|
|
import ( |
|
"log" |
|
|
|
"github.com/spf13/viper" |
|
) |
|
|
|
type Config struct { |
|
DB DBConfig |
|
Server ServerConfig |
|
Redis RedisConfig |
|
JWT JWTConfig |
|
Log LogConfig // 新增日志配置 |
|
} |
|
|
|
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 { |
|
viper.SetConfigName("config") |
|
viper.SetConfigType("yaml") |
|
viper.AddConfigPath(".") |
|
viper.AutomaticEnv() |
|
|
|
if err := viper.ReadInConfig(); err != nil { |
|
log.Fatalf("Error reading config file: %v", err) |
|
} |
|
|
|
var cfg Config |
|
if err := viper.Unmarshal(&cfg); err != nil { |
|
log.Fatalf("Unable to decode into struct: %v", err) |
|
} |
|
|
|
return &cfg |
|
}
|
|
|