Files
go_blog/config/config.go

153 lines
3.7 KiB
Go
Raw Normal View History

2024-01-10 16:05:15 +08:00
/*
@Time : 2020/6/28 21:48
@Author : xuyiqing
@File : config.py
*/
2025-04-09 16:56:05 +08:00
package config
2024-01-10 16:05:15 +08:00
import (
2024-07-24 04:19:01 +08:00
"fmt"
2024-01-10 16:05:15 +08:00
"time"
2024-07-04 19:15:44 +08:00
2024-11-07 10:09:53 +08:00
"github.com/fsnotify/fsnotify"
2024-07-24 04:19:01 +08:00
"github.com/spf13/viper"
2024-01-10 16:05:15 +08:00
)
2025-05-21 20:38:25 +08:00
const (
EnvDevelopment = "development"
EnvProduction = "production"
)
2025-04-15 16:47:00 +08:00
type DataBaseConfig struct {
2024-01-10 16:05:15 +08:00
Host string
Port string
User string
Password string
2024-07-04 19:15:44 +08:00
DBName string
2024-01-10 16:05:15 +08:00
Charset string
Prefix string
2025-04-15 16:47:00 +08:00
Driver string
DSN string
MaxConns int `mapstructure:"max_conns"`
2024-01-10 16:05:15 +08:00
}
type Jwt struct {
SecretKey string
}
type Project struct {
2025-04-15 16:47:00 +08:00
StaticUrlMapPath []interface{}
2024-01-10 16:05:15 +08:00
TemplateGlob string
2024-07-04 19:15:44 +08:00
MediaFilePath string
2025-04-09 16:56:05 +08:00
CurrentTheme string
2024-01-10 16:05:15 +08:00
}
2025-04-15 16:47:00 +08:00
type ServerConfig struct {
2024-07-04 19:15:44 +08:00
Port string
2025-04-15 16:47:00 +08:00
EnableGzip bool `mapstructure:"enable_gzip"`
CSRFSecret string `mapstructure:"csrf_secret"`
2024-07-04 19:15:44 +08:00
ReadTimeout time.Duration
2024-01-10 16:05:15 +08:00
WriteTimeout time.Duration
}
2025-04-15 16:47:00 +08:00
type ThemeConfig struct {
Current string
AllowUpload bool `mapstructure:"allow_upload"`
}
type SecurityConfig struct {
JWTSecret string `mapstructure:"jwt_secret"`
CORSAllowedOrigins []string `mapstructure:"cors_allowed_origins"`
}
type Config struct {
Env string
Server ServerConfig
DataBase DataBaseConfig
Theme ThemeConfig
JwtSecretKey Jwt
Project Project
Security SecurityConfig
2024-01-10 16:05:15 +08:00
}
2024-07-24 04:19:01 +08:00
2025-05-27 19:24:07 +08:00
var globalConfig *Config // 新增全局配置变量
2025-04-15 16:47:00 +08:00
func LoadConfig(configPath string) (*Config, error) {
// 1. 基础配置
viper.SetConfigFile(configPath)
2024-07-24 04:19:01 +08:00
// 设置配置文件的名称(不包括扩展名)
2025-04-15 16:47:00 +08:00
viper.SetConfigName("config")
2024-07-24 04:19:01 +08:00
// 设置配置文件所在的目录
2025-04-15 16:47:00 +08:00
viper.AddConfigPath("./config")
2024-07-24 04:19:01 +08:00
// 设置配置文件的类型为YAML
viper.SetConfigType("yaml")
2025-04-15 16:47:00 +08:00
// 3. 自动读取环境变量(自动转换大写和下划线)
viper.AutomaticEnv()
viper.SetEnvPrefix("BLOG") // 环境变量前缀 BLOG_xxx
2024-07-24 04:19:01 +08:00
2025-04-15 16:47:00 +08:00
// 2. 设置默认值
viper.SetDefault("server.port", 3000)
viper.SetDefault("database.max_conns", 10)
viper.SetDefault("theme.allow_upload", false)
2024-07-24 04:19:01 +08:00
// 读取配置文件
if err := viper.ReadInConfig(); err != nil {
2025-04-15 16:47:00 +08:00
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
return nil, fmt.Errorf("配置文件未找到: %s", configPath)
}
return nil, fmt.Errorf("读取配置文件失败: %w", err)
2024-07-24 04:19:01 +08:00
}
2025-04-15 16:47:00 +08:00
//监控并重新读取配置文件
2024-11-07 10:09:53 +08:00
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
// 配置文件发生变更之后会调用的回调函数
fmt.Println("Config file changed:", e.Name)
})
2025-04-15 16:47:00 +08:00
// 5. 反序列化到结构体
var cfg Config
if err := viper.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("配置解析失败: %w", err)
}
// 6. 校验必要配置项
// if cfg.Security.JWTSecret == "" {
// return nil, fmt.Errorf("安全配置错误: jwt_secret 必须设置")
// }
2024-07-24 04:19:01 +08:00
// 获取配置值
2025-04-15 16:47:00 +08:00
mysqlHost := viper.GetString("database.Host")
2024-07-24 04:19:01 +08:00
jwtSecretKey := viper.GetString("jwt.SecretKey")
serverPort := viper.GetInt("server.Port")
2024-08-01 02:44:06 +08:00
templateGlob := viper.GetString("project.TemplateGlob")
2024-07-24 04:19:01 +08:00
// 打印获取到的配置值
fmt.Printf("MySQL Host: %s\n", mysqlHost)
fmt.Printf("JWT Secret Key: %s\n", jwtSecretKey)
fmt.Printf("Server Port: %d\n", serverPort)
2024-08-01 02:44:06 +08:00
fmt.Printf("TemplateGlob: %s\n", templateGlob)
2025-04-15 16:47:00 +08:00
2025-05-27 19:24:07 +08:00
globalConfig = &cfg // 保存配置到全局变量
2025-04-15 16:47:00 +08:00
return &cfg, nil
2024-07-24 04:19:01 +08:00
}
2025-04-15 16:47:00 +08:00
2025-04-09 16:56:05 +08:00
func GetCurrentTheme() string {
return "default"
}
2025-06-09 17:59:19 +08:00
func SetCurrentTheme(theme string) error {
return nil
2025-04-09 16:56:05 +08:00
}
2025-05-27 19:01:05 +08:00
// GetJWTSecret 获取 JWT 密钥
2025-05-27 19:24:07 +08:00
// 移除原有的方法定义(如果存在)
// func (c *Config) GetJWTSecret() string {
// 原错误:返回 security.JWTSecret实际应使用 jwtSecretKey.SecretKey
// }
// 新增包级函数获取 JWT 密钥
func GetJWTSecret() string {
2025-06-09 17:59:19 +08:00
if globalConfig == nil {
//panic("配置未加载,请先调用 LoadConfig")
}
return globalConfig.JwtSecretKey.SecretKey
2025-05-27 19:01:05 +08:00
}