first commit

This commit is contained in:
何昌清
2026-03-26 22:13:03 +08:00
parent bbe1faa363
commit a2685f7f1e
51 changed files with 11244 additions and 0 deletions

107
config/config.go Normal file
View 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
}

33
config/default.yaml Normal file
View File

@@ -0,0 +1,33 @@
# 服务器配置
server:
port: "8080"
mode: "debug"
# JWT配置
jwt:
secret: "admin-test"
expire: 24h
# 数据库配置
db:
host: "localhost"
port: "3306"
user: "root"
password: "123456"
name: "quincy"
# Redis配置
redis:
host: "localhost"
port: "6379"
password: ""
db: "0"
# mmdb文件位置
mmdb:
path: "/home/app/quincy/GeoLite2-City.mmdb"
# 日志文件位置
log:
path: "/home/app/quincy"