作者 yangfu

cros修改

... ... @@ -2,19 +2,12 @@ package beego
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/plugins/cors"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/middleware"
_ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/routers"
)
func init() {
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin"},
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin"},
AllowCredentials: true,
}))
beego.InsertFilter("*", beego.BeforeRouter, middleware.AllowCors())
beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequstLogFilter())
beego.InsertFilter("/*", beego.AfterExec, middleware.CreateResponseLogFilter(), false)
... ...
... ... @@ -5,10 +5,12 @@ import (
"errors"
"fmt"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/plugins/cors"
"github.com/tiptok/gocomm/common"
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/cachex"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
"net/http"
"strconv"
"strings"
"time"
... ... @@ -120,3 +122,22 @@ func CreateResponseLogFilter() func(ctx *context.Context) {
log.Debug(fmt.Sprintf("<====Send User:%v RequestId:%v BodyData:%s", ctx.Input.GetData("x-mmm-id"), requestId, body))
}
}
func AllowCors() func(ctx *context.Context) {
return func(ctx *context.Context) {
if ctx.Request.Method != "OPTIONS" {
return
}
f := cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Content-Type", "x-requested-with"},
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin"},
AllowCredentials: true,
})
f(ctx)
ctx.Output.Body([]byte("{}"))
ctx.Output.SetStatus(http.StatusNoContent)
return
}
}
... ...