103 lines
2.6 KiB
Go
103 lines
2.6 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"gopkg.in/yaml.v3"
|
|
"os"
|
|
)
|
|
|
|
var MineConfig *Config
|
|
|
|
// Config 应用配置结构
|
|
type Config struct {
|
|
App AppConfig `yaml:"app"`
|
|
Database DatabaseConfig `yaml:"database"`
|
|
JWT JWTConfig `yaml:"jwt"`
|
|
Server ServerConfig `yaml:"server"`
|
|
Log LogConfig `yaml:"log"`
|
|
}
|
|
|
|
// AppConfig 应用基础配置
|
|
type AppConfig struct {
|
|
Name string `yaml:"name"`
|
|
Version string `yaml:"version"`
|
|
Port int `yaml:"port"`
|
|
Env string `yaml:"env"`
|
|
}
|
|
|
|
// DatabaseConfig 数据库配置
|
|
type DatabaseConfig struct {
|
|
Driver string `yaml:"driver"`
|
|
Host string `yaml:"host"`
|
|
Port int `yaml:"port"`
|
|
Username string `yaml:"username"`
|
|
Password string `yaml:"password"`
|
|
DBName string `yaml:"dbname"`
|
|
Charset string `yaml:"charset"`
|
|
SQLitePath string `yaml:"sqlite_path"`
|
|
MaxIdleConns int `yaml:"max_idle_conns"`
|
|
MaxOpenConns int `yaml:"max_open_conns"`
|
|
ConnMaxLifetime int `yaml:"conn_max_lifetime"`
|
|
}
|
|
|
|
// JWTConfig JWT配置
|
|
type JWTConfig struct {
|
|
SecretKey string `yaml:"secret_key"`
|
|
ExpireHours int `yaml:"expire_hours"`
|
|
}
|
|
|
|
// ServerConfig 服务器配置
|
|
type ServerConfig struct {
|
|
ReadTimeout int `yaml:"read_timeout"`
|
|
WriteTimeout int `yaml:"write_timeout"`
|
|
StaticPath string `yaml:"static_path"`
|
|
UploadMaxSize int `yaml:"upload_max_size"`
|
|
}
|
|
|
|
// LogConfig 日志配置
|
|
type LogConfig struct {
|
|
Level string `yaml:"level"`
|
|
FilePath string `yaml:"file_path"`
|
|
MaxSize int `yaml:"max_size"`
|
|
MaxAge int `yaml:"max_age"`
|
|
MaxBackups int `yaml:"max_backups"`
|
|
}
|
|
|
|
// LoadConfig 加载配置文件
|
|
func LoadConfig(configPath string) (*Config, error) {
|
|
config := &Config{}
|
|
|
|
// 读取配置文件
|
|
file, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("读取配置文件失败: %v", err)
|
|
}
|
|
|
|
// 解析YAML
|
|
err = yaml.Unmarshal(file, config)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("解析配置文件失败: %v", err)
|
|
}
|
|
|
|
// 设置全局配置
|
|
MineConfig = config
|
|
|
|
return config, nil
|
|
}
|
|
|
|
// GetDSN 获取数据库连接字符串
|
|
func (db *DatabaseConfig) GetDSN() string {
|
|
switch db.Driver {
|
|
case "mysql":
|
|
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
|
|
db.Username, db.Password, db.Host, db.Port, db.DBName, db.Charset)
|
|
case "postgres":
|
|
return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai",
|
|
db.Host, db.Username, db.Password, db.DBName, db.Port)
|
|
case "sqlite":
|
|
return db.SQLitePath
|
|
default:
|
|
return ""
|
|
}
|
|
}
|