调整工具类的方法,优化方法调用及增加迁移工具及其用法
This commit is contained in:
@@ -43,6 +43,36 @@ func DefaultCORSConfig() *CORSConfig {
|
||||
}
|
||||
}
|
||||
|
||||
// NewCORSConfig 从配置参数创建 CORSConfig
|
||||
// 用于从 config 包的 CORSConfig 转换为 middleware 的 CORSConfig
|
||||
// 避免循环依赖
|
||||
func NewCORSConfig(allowedOrigins, allowedMethods, allowedHeaders, exposedHeaders []string, allowCredentials bool, maxAge int) *CORSConfig {
|
||||
cfg := &CORSConfig{
|
||||
AllowedOrigins: allowedOrigins,
|
||||
AllowedMethods: allowedMethods,
|
||||
AllowedHeaders: allowedHeaders,
|
||||
ExposedHeaders: exposedHeaders,
|
||||
AllowCredentials: allowCredentials,
|
||||
MaxAge: maxAge,
|
||||
}
|
||||
|
||||
// 设置默认值(如果为空)
|
||||
if len(cfg.AllowedOrigins) == 0 {
|
||||
cfg.AllowedOrigins = []string{"*"}
|
||||
}
|
||||
if len(cfg.AllowedMethods) == 0 {
|
||||
cfg.AllowedMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"}
|
||||
}
|
||||
if len(cfg.AllowedHeaders) == 0 {
|
||||
cfg.AllowedHeaders = []string{"Content-Type", "Authorization", "X-Requested-With", "X-Timezone"}
|
||||
}
|
||||
if cfg.MaxAge == 0 {
|
||||
cfg.MaxAge = 86400
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
// CORS CORS中间件
|
||||
func CORS(config ...*CORSConfig) func(http.Handler) http.Handler {
|
||||
var cfg *CORSConfig
|
||||
|
||||
Reference in New Issue
Block a user