This commit is contained in:
@@ -96,6 +96,7 @@ type LogRotationConfig struct {
|
||||
type MiddlewareConfig struct {
|
||||
EnableRateLimiter bool `mapstructure:"enable_rate_limiter"` // 启用限流器(默认:false)
|
||||
RateLimiter RateLimiterConfig `mapstructure:"rate_limiter"` // 限流器配置
|
||||
CORS CORSConfig `mapstructure:"cors"` // 跨域配置
|
||||
}
|
||||
|
||||
// ClientConfig 客户端行为开关配置
|
||||
@@ -110,6 +111,17 @@ type RateLimiterConfig struct {
|
||||
Storage string `mapstructure:"storage"` // "memory" 或 "redis"
|
||||
}
|
||||
|
||||
// CORSConfig 跨域资源共享配置
|
||||
type CORSConfig struct {
|
||||
Enabled bool `mapstructure:"enabled"` // 是否启用 CORS
|
||||
AllowOrigins string `mapstructure:"allow_origins"` // 允许的来源,多个来源用逗号分隔
|
||||
AllowMethods string `mapstructure:"allow_methods"` // 允许的 HTTP 方法,多个方法用逗号分隔
|
||||
AllowHeaders string `mapstructure:"allow_headers"` // 允许的请求头,多个请求头用逗号分隔
|
||||
ExposeHeaders string `mapstructure:"expose_headers"` // 暴露给浏览器读取的响应头,多个响应头用逗号分隔
|
||||
AllowCredentials bool `mapstructure:"allow_credentials"` // 是否允许携带凭证
|
||||
MaxAge int `mapstructure:"max_age"` // 预检结果缓存秒数
|
||||
}
|
||||
|
||||
// SMSConfig 短信服务配置
|
||||
type SMSConfig struct {
|
||||
GatewayURL string `mapstructure:"gateway_url"` // 短信网关地址
|
||||
@@ -282,6 +294,20 @@ func (c *Config) Validate() error {
|
||||
if c.Middleware.RateLimiter.Storage != "" && !validStorage[c.Middleware.RateLimiter.Storage] {
|
||||
return fmt.Errorf("invalid configuration: middleware.rate_limiter.storage: invalid storage type (current value: %s, expected: memory or redis)", c.Middleware.RateLimiter.Storage)
|
||||
}
|
||||
if c.Middleware.CORS.Enabled {
|
||||
if strings.TrimSpace(c.Middleware.CORS.AllowOrigins) == "" {
|
||||
return fmt.Errorf("invalid configuration: middleware.cors.allow_origins: must be non-empty when CORS is enabled")
|
||||
}
|
||||
if c.Middleware.CORS.AllowCredentials && strings.TrimSpace(c.Middleware.CORS.AllowOrigins) == "*" {
|
||||
return fmt.Errorf("invalid configuration: middleware.cors.allow_origins: wildcard origin is not allowed when allow_credentials is true")
|
||||
}
|
||||
if strings.TrimSpace(c.Middleware.CORS.AllowMethods) == "" {
|
||||
return fmt.Errorf("invalid configuration: middleware.cors.allow_methods: must be non-empty when CORS is enabled")
|
||||
}
|
||||
if c.Middleware.CORS.MaxAge < 0 {
|
||||
return fmt.Errorf("invalid configuration: middleware.cors.max_age: must be greater than or equal to 0")
|
||||
}
|
||||
}
|
||||
|
||||
// 短信服务验证(可选,配置 GatewayURL 时才验证其他字段)
|
||||
if c.SMS.GatewayURL != "" {
|
||||
|
||||
Reference in New Issue
Block a user