Files
go_blog/config/config.go

108 lines
2.5 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
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 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 {
CORSAllowedOrigins []string `mapstructure:"cors_allowed_origins"`
}
type Config struct {
2025-06-18 18:34:08 +08:00
Env string
Server ServerConfig
DataBase DataBaseConfig
Theme ThemeConfig
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)
}
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
}
// GetGlobalConfig 获取全局配置
func GetGlobalConfig() *Config {
return globalConfig
}