...
|
...
|
@@ -6,12 +6,14 @@ import ( |
|
|
"github.com/beego/beego/v2/server/web"
|
|
|
"github.com/beego/beego/v2/server/web/context"
|
|
|
"github.com/linmadan/egglib-go/web/beego/filters"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/port/beego/controllers"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
. "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
|
|
|
_ "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/port/beego/routers"
|
...
|
...
|
@@ -53,9 +55,13 @@ func init() { |
|
|
|
|
|
web.InsertFilter("/*", web.BeforeRouter, filters.AllowCors())
|
|
|
web.InsertFilter("/*", web.BeforeRouter, JwtFilter())
|
|
|
web.InsertFilter("/*", web.BeforeRouter, RequestCostBefore())
|
|
|
web.InsertFilter("/*", web.BeforeExec, controllers.BlacklistFilter(controllers.BlacklistRouters))
|
|
|
web.InsertFilter("/*", web.BeforeExec, CreateRequestLogFilter(true)) // filters.CreateRequstLogFilter(Logger)
|
|
|
web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(Logger), web.WithReturnOnOutput(false))
|
|
|
if constant.SERVICE_ENV == "dev" { //|| web.BConfig.RunMode =="test"
|
|
|
web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(Logger), web.WithReturnOnOutput(false))
|
|
|
}
|
|
|
web.InsertFilter("/*", web.AfterExec, RequestCostAfter(150), web.WithReturnOnOutput(false))
|
|
|
}
|
|
|
|
|
|
func CreateRequestLogFilter(console bool) func(ctx *context.Context) {
|
...
|
...
|
@@ -89,3 +95,23 @@ func JwtFilter() func(ctx *context.Context) { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
func RequestCostBefore() func(ctx *context.Context) {
|
|
|
return func(ctx *context.Context) {
|
|
|
ctx.Input.SetData("cost-begin", time.Now().UnixMilli())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func RequestCostAfter(maxCost int64) func(ctx *context.Context) {
|
|
|
return func(ctx *context.Context) {
|
|
|
t := ctx.Input.GetData("cost-begin")
|
|
|
if t != nil {
|
|
|
costBegin := t.(int64)
|
|
|
costEnd := time.Now().UnixMilli()
|
|
|
cost := costEnd - costBegin
|
|
|
if cost > 0 && maxCost > 0 && cost > maxCost {
|
|
|
msg := fmt.Sprintf("beego | %v | %v | 耗时:%v \n %v", ctx.Input.Method(), ctx.Input.URL(), time.Duration(cost)*time.Millisecond, string(ctx.Input.RequestBody))
|
|
|
logs.Warn(msg)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|