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

update

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