作者 yangfu

fix: cors refactor

@@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ import (
7 "fmt" 7 "fmt"
8 "github.com/astaxie/beego/plugins/cors" 8 "github.com/astaxie/beego/plugins/cors"
9 "gitlab.fjmaimaimai.com/mmm-go/gocomm/time" 9 "gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
  10 + "net/http"
10 protocol "openapi/pkg/domain" 11 protocol "openapi/pkg/domain"
11 "strconv" 12 "strconv"
12 "strings" 13 "strings"
@@ -179,3 +180,18 @@ func (this *BaseController) Finish() { @@ -179,3 +180,18 @@ func (this *BaseController) Finish() {
179 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))) 180 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)))
180 } 181 }
181 } 182 }
  183 +
  184 +func AllowCors() func(ctx *context.Context) {
  185 + return func(ctx *context.Context) {
  186 + ctx.Output.Header("Access-Control-Allow-Methods", "OPTIONS,DELETE,POST,GET,PUT,PATCH")
  187 + //ctx.Output.Header("Access-Control-Max-Age", "3600")
  188 + ctx.Output.Header("Access-Control-Allow-Headers", "*")
  189 + ctx.Output.Header("Access-Control-Allow-Credentials", "true")
  190 + ctx.Output.Header("Access-Control-Allow-Origin", "*") //origin
  191 + if ctx.Input.Method() == http.MethodOptions {
  192 + // options请求,返回200
  193 + ctx.Output.SetStatus(http.StatusOK)
  194 + _ = ctx.Output.Body([]byte("options support"))
  195 + }
  196 + }
  197 +}
@@ -16,7 +16,9 @@ func init() { @@ -16,7 +16,9 @@ func init() {
16 beego.NSNamespace("push", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.PushController{})), 16 beego.NSNamespace("push", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.PushController{})),
17 beego.NSNamespace("sms", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.SmsController{})), 17 beego.NSNamespace("sms", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.SmsController{})),
18 ) 18 )
19 - beego.SetStaticPath("/log/NIONkenfieldon", constant.LogFilePath) 19 + beego.SetStaticPath("/log", constant.LogFilePath)
20 beego.Router("/v2/vod/createStsAuth", &v2.VodController{}, "Post:CreateStsAuth") 20 beego.Router("/v2/vod/createStsAuth", &v2.VodController{}, "Post:CreateStsAuth")
21 beego.AddNamespace(nsV1) 21 beego.AddNamespace(nsV1)
  22 +
  23 + beego.InsertFilter("/*", beego.BeforeRouter, controllers.AllowCors())
22 } 24 }