调整工厂模式的方法
This commit is contained in:
@@ -9,14 +9,15 @@ import (
|
||||
|
||||
// Config 应用配置
|
||||
type Config struct {
|
||||
Database *DatabaseConfig `json:"database"`
|
||||
OSS *OSSConfig `json:"oss"`
|
||||
Redis *RedisConfig `json:"redis"`
|
||||
CORS *CORSConfig `json:"cors"`
|
||||
MinIO *MinIOConfig `json:"minio"`
|
||||
Email *EmailConfig `json:"email"`
|
||||
SMS *SMSConfig `json:"sms"`
|
||||
Logger *LoggerConfig `json:"logger"`
|
||||
Database *DatabaseConfig `json:"database"`
|
||||
OSS *OSSConfig `json:"oss"`
|
||||
Redis *RedisConfig `json:"redis"`
|
||||
CORS *CORSConfig `json:"cors"`
|
||||
MinIO *MinIOConfig `json:"minio"`
|
||||
Email *EmailConfig `json:"email"`
|
||||
SMS *SMSConfig `json:"sms"`
|
||||
Logger *LoggerConfig `json:"logger"`
|
||||
RateLimit *RateLimitConfig `json:"rateLimit"`
|
||||
}
|
||||
|
||||
// DatabaseConfig 数据库配置
|
||||
@@ -244,6 +245,24 @@ type LoggerConfig struct {
|
||||
BufferSize int `json:"bufferSize"`
|
||||
}
|
||||
|
||||
// RateLimitConfig 限流配置
|
||||
type RateLimitConfig struct {
|
||||
// Enable 是否启用限流
|
||||
Enable bool `json:"enable"`
|
||||
|
||||
// Rate 每个时间窗口允许的请求数量
|
||||
Rate int `json:"rate"`
|
||||
|
||||
// Period 时间窗口(秒)
|
||||
Period int `json:"period"`
|
||||
|
||||
// ByIP 按IP限流
|
||||
ByIP bool `json:"byIP"`
|
||||
|
||||
// ByUserID 按用户ID限流(从X-User-ID header获取)
|
||||
ByUserID bool `json:"byUserID"`
|
||||
}
|
||||
|
||||
// LoadFromFile 从文件加载配置
|
||||
// filePath: 配置文件路径(支持绝对路径和相对路径)
|
||||
func LoadFromFile(filePath string) (*Config, error) {
|
||||
@@ -378,6 +397,19 @@ func (c *Config) setDefaults() {
|
||||
c.Logger.Output = "stdout"
|
||||
}
|
||||
}
|
||||
|
||||
// 限流默认值
|
||||
if c.RateLimit != nil {
|
||||
if c.RateLimit.Rate == 0 {
|
||||
c.RateLimit.Rate = 100 // 默认每个窗口100个请求
|
||||
}
|
||||
if c.RateLimit.Period == 0 {
|
||||
c.RateLimit.Period = 60 // 默认时间窗口60秒
|
||||
}
|
||||
if !c.RateLimit.ByIP && !c.RateLimit.ByUserID {
|
||||
c.RateLimit.ByIP = true // 默认按IP限流
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetDatabase 获取数据库配置
|
||||
|
||||
@@ -78,6 +78,13 @@
|
||||
"disableTimestamp": false,
|
||||
"async": false,
|
||||
"bufferSize": 1000
|
||||
},
|
||||
"rateLimit": {
|
||||
"enable": true,
|
||||
"rate": 100,
|
||||
"period": 60,
|
||||
"byIP": true,
|
||||
"byUserID": false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user