Add internal/config/config.go
This commit is contained in:
197
internal/config/config.go
Normal file
197
internal/config/config.go
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config 全局配置根结构
|
||||||
|
type Config struct {
|
||||||
|
Server ServerConfig `yaml:"server"`
|
||||||
|
Database DatabaseConfig `yaml:"database"`
|
||||||
|
JWT JWTConfig `yaml:"jwt"`
|
||||||
|
Upload UploadConfig `yaml:"upload"`
|
||||||
|
Storage StorageConfig `yaml:"storage"`
|
||||||
|
SMS SMSConfig `yaml:"sms"`
|
||||||
|
Wechat WechatConfig `yaml:"wechat"`
|
||||||
|
AI AIConfig `yaml:"ai"`
|
||||||
|
CORS CORSConfig `yaml:"cors"`
|
||||||
|
RateLimit RateLimitConfig `yaml:"rate_limit"`
|
||||||
|
Env EnvConfig `yaml:"env"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServerConfig 服务配置
|
||||||
|
type ServerConfig struct {
|
||||||
|
Port int `yaml:"port"`
|
||||||
|
ReadTimeout int `yaml:"read_timeout"` // 读超时(秒)
|
||||||
|
WriteTimeout int `yaml:"write_timeout"` // 写超时(秒)
|
||||||
|
IdleTimeout int `yaml:"idle_timeout"` // 空闲超时(秒)
|
||||||
|
ShutdownTimeout int `yaml:"shutdown_timeout"` // 优雅关机等待(秒)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DatabaseConfig 数据库配置
|
||||||
|
type DatabaseConfig struct {
|
||||||
|
Driver string `yaml:"driver"` // sqlite | mysql
|
||||||
|
AutoMigrate bool `yaml:"auto_migrate"`
|
||||||
|
InitData bool `yaml:"init_data"`
|
||||||
|
SQLite SQLiteConfig `yaml:"sqlite"`
|
||||||
|
MySQL MySQLConfig `yaml:"mysql"`
|
||||||
|
DSN string `yaml:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SQLiteConfig SQLite 配置
|
||||||
|
type SQLiteConfig struct {
|
||||||
|
Path string `yaml:"path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// MySQLConfig MySQL 配置
|
||||||
|
type MySQLConfig struct {
|
||||||
|
Host string `yaml:"host"`
|
||||||
|
Port int `yaml:"port"`
|
||||||
|
Username string `yaml:"username"`
|
||||||
|
Password string `yaml:"password"`
|
||||||
|
Database string `yaml:"database"`
|
||||||
|
Charset string `yaml:"charset"`
|
||||||
|
MaxIdleConns int `yaml:"max_idle_conns"`
|
||||||
|
MaxOpenConns int `yaml:"max_open_conns"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildDSN 根据驱动构建数据源名
|
||||||
|
func (c *DatabaseConfig) BuildDSN() {
|
||||||
|
switch c.Driver {
|
||||||
|
case "mysql":
|
||||||
|
c.DSN = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
|
||||||
|
c.MySQL.Username, c.MySQL.Password, c.MySQL.Host, c.MySQL.Port, c.MySQL.Database, c.MySQL.Charset)
|
||||||
|
default:
|
||||||
|
c.DSN = c.SQLite.Path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// JWTConfig JWT 认证配置
|
||||||
|
type JWTConfig struct {
|
||||||
|
Secret string `yaml:"secret"`
|
||||||
|
ExpireHours int `yaml:"expire_hours"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UploadConfig 文件上传配置
|
||||||
|
type UploadConfig struct {
|
||||||
|
Path string `yaml:"path"`
|
||||||
|
BaseURL string `yaml:"base_url"`
|
||||||
|
MaxSize int64 `yaml:"max_size"`
|
||||||
|
AllowedTypes []string `yaml:"allowed_types"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// StorageConfig 存储配置
|
||||||
|
type StorageConfig struct {
|
||||||
|
Type string `yaml:"type"` // local | minio | cos
|
||||||
|
Local LocalStorage `yaml:"local"`
|
||||||
|
MinIO MinIOConfig `yaml:"minio"`
|
||||||
|
COS COSConfig `yaml:"cos"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// LocalStorage 本地存储配置
|
||||||
|
type LocalStorage struct {
|
||||||
|
Path string `yaml:"path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// MinIOConfig MinIO 配置
|
||||||
|
type MinIOConfig struct {
|
||||||
|
Endpoint string `yaml:"endpoint"`
|
||||||
|
AccessKey string `yaml:"access_key"`
|
||||||
|
SecretKey string `yaml:"secret_key"`
|
||||||
|
Bucket string `yaml:"bucket"`
|
||||||
|
UseSSL bool `yaml:"use_ssl"`
|
||||||
|
PublicURL string `yaml:"public_url"`
|
||||||
|
AutoCreateBucket bool `yaml:"auto_create_bucket"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// COSConfig 腾讯云 COS 配置
|
||||||
|
type COSConfig struct {
|
||||||
|
SecretID string `yaml:"secret_id"`
|
||||||
|
SecretKey string `yaml:"secret_key"`
|
||||||
|
Bucket string `yaml:"bucket"`
|
||||||
|
Region string `yaml:"region"`
|
||||||
|
BaseURL string `yaml:"base_url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SMSConfig 短信服务配置
|
||||||
|
type SMSConfig struct {
|
||||||
|
DevMode bool `yaml:"dev_mode"`
|
||||||
|
Provider string `yaml:"provider"` // mock | aliyun | tencent
|
||||||
|
CodeExpire int `yaml:"code_expire"`
|
||||||
|
SendInterval int `yaml:"send_interval"`
|
||||||
|
Aliyun AliyunSMS `yaml:"aliyun"`
|
||||||
|
Tencent TencentSMS `yaml:"tencent"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AliyunSMS 阿里云短信配置
|
||||||
|
type AliyunSMS struct {
|
||||||
|
AccessKeyID string `yaml:"access_key_id"`
|
||||||
|
AccessKeySecret string `yaml:"access_key_secret"`
|
||||||
|
SignName string `yaml:"sign_name"`
|
||||||
|
TemplateCode string `yaml:"template_code"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// TencentSMS 腾讯云短信配置
|
||||||
|
type TencentSMS struct {
|
||||||
|
SecretID string `yaml:"secret_id"`
|
||||||
|
SecretKey string `yaml:"secret_key"`
|
||||||
|
SignName string `yaml:"sign_name"`
|
||||||
|
TemplateID string `yaml:"template_id"`
|
||||||
|
AppID string `yaml:"app_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// WechatConfig 微信配置
|
||||||
|
type WechatConfig struct {
|
||||||
|
AppID string `yaml:"app_id"`
|
||||||
|
AppSecret string `yaml:"app_secret"`
|
||||||
|
MchID string `yaml:"mch_id"`
|
||||||
|
APIKey string `yaml:"api_key"`
|
||||||
|
NotifyURL string `yaml:"notify_url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AIConfig AI 配置
|
||||||
|
type AIConfig struct {
|
||||||
|
APIKey string `yaml:"api_key"`
|
||||||
|
APIType string `yaml:"api_type"` // deepseek | openai
|
||||||
|
Model string `yaml:"model"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CORSConfig CORS 跨域配置
|
||||||
|
type CORSConfig struct {
|
||||||
|
AllowedOrigins []string `yaml:"allowed_origins"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RateLimitConfig API 限流配置
|
||||||
|
type RateLimitConfig struct {
|
||||||
|
Enabled bool `yaml:"enabled"`
|
||||||
|
RequestsPerSecond int `yaml:"requests_per_second"`
|
||||||
|
BurstSize int `yaml:"burst_size"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnvConfig 环境配置
|
||||||
|
type EnvConfig struct {
|
||||||
|
Name string `yaml:"name"` // dev | prod
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppConfig 全局配置实例
|
||||||
|
var AppConfig *Config
|
||||||
|
|
||||||
|
// Load 从指定路径加载配置文件
|
||||||
|
func Load(path string) (*Config, error) {
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("读取配置文件失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var cfg Config
|
||||||
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||||
|
return nil, fmt.Errorf("解析配置文件失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.Database.BuildDSN()
|
||||||
|
AppConfig = &cfg
|
||||||
|
return &cfg, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user