重构项目的实现,优化使用方法与使用逻辑

This commit is contained in:
2026-06-25 00:03:59 +08:00
parent a6e8101e09
commit 6072ec57e8
49 changed files with 1663 additions and 12534 deletions

View File

@@ -18,9 +18,16 @@ type Config struct {
Email *EmailConfig `json:"email"`
SMS *SMSConfig `json:"sms"`
Logger *LoggerConfig `json:"logger"`
I18n *I18nConfig `json:"i18n"`
RateLimit *RateLimitConfig `json:"rateLimit"`
}
// I18nConfig 国际化配置
type I18nConfig struct {
DefaultLang string `json:"defaultLang"`
LocalesDir string `json:"localesDir"`
}
// LocalStorageConfig 本地存储配置
// 用于将文件保存到本地文件夹(适合开发环境、单机部署等场景)
type LocalStorageConfig struct {
@@ -208,6 +215,23 @@ type EmailConfig struct {
// Timeout 连接超时时间(秒)
Timeout int `json:"timeout"`
// Async 是否异步发送(默认 true省略时启用
Async *bool `json:"async"`
// Workers 异步 worker 数量(默认 2
Workers int `json:"workers"`
// QueueSize 异步队列大小(默认 1000
QueueSize int `json:"queueSize"`
}
// IsAsync 是否启用异步(默认 true
func (c *EmailConfig) IsAsync() bool {
if c == nil || c.Async == nil {
return true
}
return *c.Async
}
// SMSConfig 短信配置(阿里云短信)
@@ -232,11 +256,28 @@ type SMSConfig struct {
// Timeout 请求超时时间(秒)
Timeout int `json:"timeout"`
// Async 是否异步发送(默认 true省略时启用
Async *bool `json:"async"`
// Workers 异步 worker 数量(默认 2
Workers int `json:"workers"`
// QueueSize 异步队列大小(默认 1000
QueueSize int `json:"queueSize"`
}
// IsAsync 是否启用异步(默认 true
func (c *SMSConfig) IsAsync() bool {
if c == nil || c.Async == nil {
return true
}
return *c.Async
}
// LoggerConfig 日志配置
type LoggerConfig struct {
// Level 日志级别: debug, info, warn, error
// Level 日志级别: debug, info, error
Level string `json:"level"`
// Output 输出方式: stdout, stderr, file, both
@@ -251,16 +292,21 @@ type LoggerConfig struct {
// DisableTimestamp 禁用时间戳
DisableTimestamp bool `json:"disableTimestamp"`
// Async 是否使用异步模式(默认false即同步模式
// 异步模式日志写入通过channel异步处理不阻塞调用方
// 同步模式:日志直接写入,会阻塞调用方直到写入完成
Async bool `json:"async"`
// Async 是否使用异步模式(默认 true省略时启用
Async *bool `json:"async"`
// BufferSize 异步模式下的缓冲区大小默认1000
// 当缓冲区满时,新的日志会阻塞直到有空间
BufferSize int `json:"bufferSize"`
}
// IsAsync 是否启用异步(默认 true
func (c *LoggerConfig) IsAsync() bool {
if c == nil || c.Async == nil {
return true
}
return *c.Async
}
// RateLimitConfig 限流配置
type RateLimitConfig struct {
// Enable 是否启用限流
@@ -384,13 +430,19 @@ func (c *Config) setDefaults() {
// 邮件默认值
if c.Email != nil {
if c.Email.Port == 0 {
c.Email.Port = 587 // 默认使用587端口TLS
c.Email.Port = 587
}
if c.Email.From == "" {
c.Email.From = c.Email.Username
}
if c.Email.Timeout == 0 {
c.Email.Timeout = 30
c.Email.Timeout = 5
}
if c.Email.Workers == 0 {
c.Email.Workers = 2
}
if c.Email.QueueSize == 0 {
c.Email.QueueSize = 1000
}
}
@@ -400,7 +452,13 @@ func (c *Config) setDefaults() {
c.SMS.Region = "cn-hangzhou"
}
if c.SMS.Timeout == 0 {
c.SMS.Timeout = 10
c.SMS.Timeout = 5
}
if c.SMS.Workers == 0 {
c.SMS.Workers = 2
}
if c.SMS.QueueSize == 0 {
c.SMS.QueueSize = 1000
}
}
@@ -412,6 +470,14 @@ func (c *Config) setDefaults() {
if c.Logger.Output == "" {
c.Logger.Output = "stdout"
}
if c.Logger.BufferSize == 0 {
c.Logger.BufferSize = 1000
}
}
// i18n 默认值
if c.I18n != nil && c.I18n.DefaultLang == "" {
c.I18n.DefaultLang = "zh-CN"
}
// 限流默认值
@@ -475,6 +541,11 @@ func (c *Config) GetLogger() *LoggerConfig {
return c.Logger
}
// GetI18n 获取国际化配置
func (c *Config) GetI18n() *I18nConfig {
return c.I18n
}
// GetDatabaseDSN 获取数据库连接字符串
func (c *Config) GetDatabaseDSN() (string, error) {
if c.Database == nil {
@@ -549,3 +620,8 @@ func (c *Config) GetRedisAddr() string {
// 构建地址
return fmt.Sprintf("%s:%d", c.Redis.Host, c.Redis.Port)
}
// BoolPtr 返回 bool 指针(用于配置默认值)
func BoolPtr(v bool) *bool {
return &v
}