作者 yangfu

fix: cors refactor

... ... @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/astaxie/beego/plugins/cors"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
"net/http"
protocol "openapi/pkg/domain"
"strconv"
"strings"
... ... @@ -179,3 +180,18 @@ func (this *BaseController) Finish() {
log.Debug(fmt.Sprintf("<====Send to uid(%d) ucid(%v) client: %d byte\nRequestId:%s RspBodyData: %s", this.Header.Uid, this.Header.UserId, length, this.Header.GetRequestId(), string(strByte)))
}
}
func AllowCors() func(ctx *context.Context) {
return func(ctx *context.Context) {
ctx.Output.Header("Access-Control-Allow-Methods", "OPTIONS,DELETE,POST,GET,PUT,PATCH")
//ctx.Output.Header("Access-Control-Max-Age", "3600")
ctx.Output.Header("Access-Control-Allow-Headers", "*")
ctx.Output.Header("Access-Control-Allow-Credentials", "true")
ctx.Output.Header("Access-Control-Allow-Origin", "*") //origin
if ctx.Input.Method() == http.MethodOptions {
// options请求,返回200
ctx.Output.SetStatus(http.StatusOK)
_ = ctx.Output.Body([]byte("options support"))
}
}
}
... ...
... ... @@ -16,7 +16,9 @@ func init() {
beego.NSNamespace("push", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.PushController{})),
beego.NSNamespace("sms", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.SmsController{})),
)
beego.SetStaticPath("/log/NIONkenfieldon", constant.LogFilePath)
beego.SetStaticPath("/log", constant.LogFilePath)
beego.Router("/v2/vod/createStsAuth", &v2.VodController{}, "Post:CreateStsAuth")
beego.AddNamespace(nsV1)
beego.InsertFilter("/*", beego.BeforeRouter, controllers.AllowCors())
}
... ...