作者 yangfu

cros修改

@@ -2,19 +2,12 @@ package beego @@ -2,19 +2,12 @@ package beego
2 2
3 import ( 3 import (
4 "github.com/astaxie/beego" 4 "github.com/astaxie/beego"
5 - "github.com/astaxie/beego/plugins/cors"  
6 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/middleware" 5 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/middleware"
7 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/routers" 6 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/routers"
8 ) 7 )
9 8
10 func init() { 9 func init() {
11 - beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{  
12 - AllowOrigins: []string{"*"},  
13 - AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},  
14 - AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin"},  
15 - ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin"},  
16 - AllowCredentials: true,  
17 - })) 10 + beego.InsertFilter("*", beego.BeforeRouter, middleware.AllowCors())
18 11
19 beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequstLogFilter()) 12 beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequstLogFilter())
20 beego.InsertFilter("/*", beego.AfterExec, middleware.CreateResponseLogFilter(), false) 13 beego.InsertFilter("/*", beego.AfterExec, middleware.CreateResponseLogFilter(), false)
@@ -5,10 +5,12 @@ import ( @@ -5,10 +5,12 @@ import (
5 "errors" 5 "errors"
6 "fmt" 6 "fmt"
7 "github.com/astaxie/beego/context" 7 "github.com/astaxie/beego/context"
  8 + "github.com/astaxie/beego/plugins/cors"
8 "github.com/tiptok/gocomm/common" 9 "github.com/tiptok/gocomm/common"
9 "github.com/tiptok/gocomm/pkg/log" 10 "github.com/tiptok/gocomm/pkg/log"
10 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/cachex" 11 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/cachex"
11 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol" 12 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
  13 + "net/http"
12 "strconv" 14 "strconv"
13 "strings" 15 "strings"
14 "time" 16 "time"
@@ -120,3 +122,22 @@ func CreateResponseLogFilter() func(ctx *context.Context) { @@ -120,3 +122,22 @@ func CreateResponseLogFilter() func(ctx *context.Context) {
120 log.Debug(fmt.Sprintf("<====Send User:%v RequestId:%v BodyData:%s", ctx.Input.GetData("x-mmm-id"), requestId, body)) 122 log.Debug(fmt.Sprintf("<====Send User:%v RequestId:%v BodyData:%s", ctx.Input.GetData("x-mmm-id"), requestId, body))
121 } 123 }
122 } 124 }
  125 +
  126 +func AllowCors() func(ctx *context.Context) {
  127 + return func(ctx *context.Context) {
  128 + if ctx.Request.Method != "OPTIONS" {
  129 + return
  130 + }
  131 + f := cors.Allow(&cors.Options{
  132 + AllowOrigins: []string{"*"},
  133 + AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
  134 + AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Content-Type", "x-requested-with"},
  135 + ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin"},
  136 + AllowCredentials: true,
  137 + })
  138 + f(ctx)
  139 + ctx.Output.Body([]byte("{}"))
  140 + ctx.Output.SetStatus(http.StatusNoContent)
  141 + return
  142 + }
  143 +}