提交 528a8e07 authored 作者: mooncake9527's avatar mooncake9527

update

上级 9dc31324
......@@ -241,7 +241,7 @@ type App struct {
}
type Middleware struct {
Cors bool `yaml:"cors" json:"cors"`
Cors Cors `yaml:"cors" json:"cors"`
RequestID RequestID `yaml:"requestID" json:"requestID"`
EnableCircuitBreaker bool `yaml:"enableCircuitBreaker" json:"enableCircuitBreaker"`
EnableHTTPProfile bool `yaml:"enableHTTPProfile" json:"enableHTTPProfile"`
......@@ -251,6 +251,11 @@ type Middleware struct {
Trace Trace `yaml:"trace" json:"trace"`
}
type Cors struct {
Enable bool `yaml:"enable" json:"enable"`
AllowOrigins string `yaml:"allowOrigins" json:"allowOrigins"`
}
type RequestID struct {
Key string `yaml:"key" json:"key"`
}
......
......@@ -4,7 +4,9 @@ app:
version: "v0.0.1"
host: "0.0.0.0"
middleware:
cors: false # true or false
cors:
enable: true
allowOrigins: http://wanzhuangkj.com,https://wanzhuangkj.com,http://hzqitu.com,https://hzqitu.com
requestID:
key: trace-id # string or empty
enableStat: false
......
......@@ -7,23 +7,37 @@ import (
"github.com/gin-gonic/gin"
)
// Cors cross domain
func Cors() gin.HandlerFunc {
return cors.New(
cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Content-Type", "Accept"},
ExposeHeaders: []string{"Content-Length", "text/plain", "Authorization", "Content-Type"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
},
)
type corsOptions struct {
allowOrigins []string
}
func CorsQT() gin.HandlerFunc {
type CorsOption func(*corsOptions)
func defaultCorsOptions() *corsOptions {
return &corsOptions{
allowOrigins: []string{"*"},
}
}
func (o *corsOptions) apply(opts ...CorsOption) {
for _, opt := range opts {
opt(o)
}
}
func WithCorsOption(allowOrigins []string) CorsOption {
return func(o *corsOptions) {
o.allowOrigins = allowOrigins
}
}
// Cors cross domain
func Cors(opts ...CorsOption) gin.HandlerFunc {
o := defaultCorsOptions()
o.apply(opts...)
return cors.New(
cors.Config{
AllowOrigins: []string{"http://wanzhuangkj.com", "https://wanzhuangkj.com", "http://hzqitu.com", "https://hzqitu.com"},
AllowOrigins: o.allowOrigins,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Content-Type", "Accept"},
ExposeHeaders: []string{"Content-Length", "text/plain", "Authorization", "Content-Type"},
......@@ -32,3 +46,16 @@ func CorsQT() gin.HandlerFunc {
},
)
}
// func CorsQT() gin.HandlerFunc {
// return cors.New(
// cors.Config{
// AllowOrigins: []string{"http://wanzhuangkj.com", "https://wanzhuangkj.com", "http://hzqitu.com", "https://hzqitu.com"},
// AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
// AllowHeaders: []string{"Origin", "Authorization", "Content-Type", "Accept"},
// ExposeHeaders: []string{"Content-Length", "text/plain", "Authorization", "Content-Type"},
// AllowCredentials: true,
// MaxAge: 12 * time.Hour,
// },
// )
// }
package middleware_test
......@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
......@@ -41,8 +42,13 @@ func New() *gin.Engine {
r := gin.New()
r.Use(middleware.CustomRecoveryWithLogger(logger.Get()))
if app.Middleware.Cors {
r.Use(middleware.CorsQT())
if app.Middleware.Cors.Enable {
origins := strings.Split(app.Middleware.Cors.AllowOrigins, ",")
if len(origins) > 0 {
r.Use(middleware.Cors(middleware.WithCorsOption(origins)))
} else {
r.Use(middleware.Cors())
}
}
gin.DefaultWriter = logger.GetZapWriter()
gin.DefaultErrorWriter = logger.GetZapWriter()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论