Skip to content

Commit

Permalink
fix issue gogf#2516 (gogf#2531)
Browse files Browse the repository at this point in the history
* fix issue gogf#2516

* golang ci configuration updates

* add example for default value of http request
  • Loading branch information
gqcn authored Mar 22, 2023
1 parent 676022e commit 12e9feb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ linters-settings:
goconst:
# Minimal length of string constant.
# Default: 3
min-len: 2
min-len: 4
# Minimum occurrences of constant string count to trigger issue.
# Default: 3
# For subsequent optimization, the value is reduced.
Expand Down
41 changes: 41 additions & 0 deletions example/httpserver/default_value/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)

type GetListReq struct {
g.Meta `path:"/" method:"get"`
Type string `v:"required#请选择内容模型" dc:"内容模型"`
Page int `v:"min:0#分页号码错误" dc:"分页号码" d:"1"`
Size int `v:"max:50#分页数量最大50条" dc:"分页数量,最大50" d:"10"`
Sort int `v:"in:0,1,2#排序类型不合法" dc:"排序类型(0:最新, 默认。1:活跃, 2:热度)"`
}
type GetListRes struct {
Items []Item `dc:"内容列表"`
}

type Item struct {
Id int64 `dc:"内容ID"`
Title string `dc:"内容标题"`
}

type Controller struct{}

func (Controller) GetList(ctx context.Context, req *GetListReq) (res *GetListRes, err error) {
g.Log().Info(ctx, req)
return
}

func main() {
s := g.Server()
s.Group("/content", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind(&Controller{})
})
s.SetPort(8199)
s.Run()
}
10 changes: 8 additions & 2 deletions net/ghttp/ghttp_middleware_tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"go.opentelemetry.io/otel/trace"

"github.com/gogf/gf/v2"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/httputil"
"github.com/gogf/gf/v2/internal/utils"
"github.com/gogf/gf/v2/net/gtrace"
Expand Down Expand Up @@ -80,7 +81,12 @@ func internalMiddlewareServerTracing(r *Request) {
}

// Request content logging.
reqBodyContentBytes, _ := ioutil.ReadAll(r.Body)
reqBodyContentBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
r.SetError(gerror.Wrap(err, `read request body failed`))
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err))
return
}
r.Body = utils.NewReadCloser(reqBodyContentBytes, false)

span.AddEvent(tracingEventHttpRequest, trace.WithAttributes(
Expand All @@ -97,7 +103,7 @@ func internalMiddlewareServerTracing(r *Request) {
r.Middleware.Next()

// Error logging.
if err := r.GetError(); err != nil {
if err = r.GetError(); err != nil {
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err))
}
// Response content logging.
Expand Down

0 comments on commit 12e9feb

Please sign in to comment.