作者 yangfu

1.中间间修改

... ... @@ -2,14 +2,13 @@ package appsvr
import (
"github.com/astaxie/beego"
"github.com/linmadan/egglib-go/web/beego/filters"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/middleware"
_ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/routers"
)
func init() {
beego.InsertFilter("/*", beego.BeforeExec, filters.CreateRequestBodyFilter())
beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequestBodyFilter())
beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequstLogFilter(Logger))
beego.InsertFilter("/*", beego.AfterExec, middleware.CreateResponseLogFilter(Logger), false)
}
... ...
... ... @@ -20,7 +20,7 @@ func (this *UserController) UserInfo() {
}()
var request *protocol.UserInfoRequest
if err := this.JsonUnmarshal(&request); err != nil {
msg = protocol.BadRequestParam(1)
msg = protocol.BadRequestParam(2)
return
}
if b, m := this.Valid(request); !b {
... ...
... ... @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/logs"
"io/ioutil"
"time"
)
... ... @@ -30,3 +31,20 @@ func CreateResponseLogFilter(logger *logs.BeeLogger) func(ctx *context.Context)
logger.Debug(fmt.Sprintf("<====Send RequestId:%v BodyData:%s", requestId, body))
}
}
func CreateRequestBodyFilter() func(ctx *context.Context) {
return func(ctx *context.Context) {
if len(ctx.Input.RequestBody) == 0 {
if ctx.Request.Method == "POST" || ctx.Request.Method == "PUT" {
body, _ := ioutil.ReadAll(ctx.Request.Body)
if len(body) == 0 {
body = []byte("{}")
}
ctx.Input.SetData("requestBody", body)
ctx.Request.Body.Close()
}
} else {
ctx.Input.SetData("requestBody", ctx.Input.RequestBody)
}
}
}
... ...