first commit
This commit is contained in:
107
config/config.go
Normal file
107
config/config.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// Config 结构体定义了应用程序的所有配置项
|
||||
type Config struct {
|
||||
// 数据库配置
|
||||
DB struct {
|
||||
Host string `yaml:"host"` // 数据库主机地址
|
||||
Port string `yaml:"port"` // 数据库端口
|
||||
User string `yaml:"user"` // 数据库用户名
|
||||
Password string `yaml:"password"` // 数据库密码
|
||||
Name string `yaml:"name"` // 数据库名称
|
||||
} `yaml:"db"`
|
||||
|
||||
Redis struct {
|
||||
Host string `yaml:"host"` // Redis主机地址
|
||||
Port string `yaml:"port"` // Redis端口
|
||||
Password string `yaml:"password"` // Redis密码
|
||||
DB string `yaml:"db"` // Redis数据库索引
|
||||
} `yaml:"redis"`
|
||||
|
||||
// 服务器配置
|
||||
Server struct {
|
||||
Port string `yaml:"port"` // 服务器监听端口
|
||||
Mode string `yaml:"mode"` // 运行模式
|
||||
} `yaml:"server"`
|
||||
|
||||
Jwt struct {
|
||||
Secret string `yaml:"secret"` // JWT 密钥
|
||||
Expire string `yaml:"expire"` // JWT 过期时间(单位:小时)
|
||||
} `yaml:"jwt"`
|
||||
|
||||
// mmdb 配置
|
||||
Mmdb struct {
|
||||
Path string `yaml:"path"` // mmdb 文件路径
|
||||
} `yaml:"mmdb"`
|
||||
|
||||
// 日志配置
|
||||
Log struct {
|
||||
Path string `yaml:"path"` // 日志目录路径
|
||||
} `yaml:"log"`
|
||||
}
|
||||
|
||||
// LoadConfig 加载并返回应用程序配置
|
||||
func LoadConfig() *Config {
|
||||
// 首先尝试从配置文件加载
|
||||
if cfg := loadConfigFromFile(); cfg != nil {
|
||||
return cfg
|
||||
}
|
||||
|
||||
// 如果没有配置文件,则使用环境变量或默认值
|
||||
cfg := &Config{}
|
||||
|
||||
// 数据库配置
|
||||
cfg.DB.Host = getEnv("DB_HOST", "localhost")
|
||||
cfg.DB.Port = getEnv("DB_PORT", "3306")
|
||||
cfg.DB.User = getEnv("DB_USER", "root")
|
||||
cfg.DB.Password = getEnv("DB_PASSWORD", "123456")
|
||||
cfg.DB.Name = getEnv("DB_NAME", "quincy")
|
||||
|
||||
// Redis 配置
|
||||
cfg.Redis.Host = getEnv("REDIS_HOST", "localhost")
|
||||
cfg.Redis.Port = getEnv("REDIS_PORT", "6379")
|
||||
cfg.Redis.Password = getEnv("REDIS_PASSWORD", "")
|
||||
cfg.Redis.DB = getEnv("REDIS_DB", "0")
|
||||
|
||||
// 服务器配置
|
||||
cfg.Server.Port = getEnv("SERVER_PORT", "8080")
|
||||
cfg.Server.Mode = getEnv("SERVER_MODE", "debug")
|
||||
|
||||
// Jwt 配置
|
||||
cfg.Jwt.Secret = getEnv("JWT_SECRET", "your-secret-key")
|
||||
cfg.Jwt.Expire = getEnv("JWT_EXPIRE", "24")
|
||||
|
||||
// mmdb 配置
|
||||
cfg.Mmdb.Path = getEnv("MMDB_PATH", "./GeoLite2-City.mmdb")
|
||||
|
||||
cfg.Log.Path = getEnv("LOG_DIR", "./")
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
// loadConfigFromFile 从配置文件加载配置
|
||||
func loadConfigFromFile() *Config {
|
||||
// 尝试加载 yaml 配置文件
|
||||
if data, err := os.ReadFile("/home/app/quin_admin/default.yaml"); err == nil {
|
||||
cfg := &Config{}
|
||||
if err := yaml.Unmarshal(data, cfg); err == nil {
|
||||
return cfg
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getEnv 获取环境变量,如果不存在则返回默认值
|
||||
func getEnv(key, defaultValue string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
Reference in New Issue
Block a user