Skip to content

Commit

Permalink
organise
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuyasen committed Jun 8, 2022
1 parent d91d8df commit df340d8
Show file tree
Hide file tree
Showing 33 changed files with 1,800 additions and 489 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
*.go linguist-language=Go
41 changes: 22 additions & 19 deletions README.md
@@ -1,23 +1,26 @@
## golang 常用包
# pkg

- [awss3 aws s3客户端](./awss3)
- [dingTalk 钉钉机器人客户端](./dingTalk)
- [email 发邮件客户端](./email)
- [errorcode 错误码定义](./errcode)
- [gobash 在go程序执行命令或可执行文件](./gobash)
- [goFile 文件处理](./goFile)
- [gohttp http客户端](./gohttp)
- [jwt 鉴权](./jwt)
- [krand 随机数和字符串生成器](./krand)
- [logger 日志](./logger)
- [mconf 配置处理](./mconf)
- [mongo 客户端](./mongo)
- [mysql 客户端](./mysql)
- [nats 客户端](./nats)
- [redis 客户端](./redis)
- [render gin返回数据封装](./render)
- [snowFlake id生成器](./snowFlake)
- [validator 校验](./validator)
go常用包列表

- [awss3 aws s3客户端](awss3)
- [dingTalk 钉钉机器人客户端](dingTalk)
- [email 发邮件客户端](email)
- [errorcode 错误码定义](gin/errcode)
- [middleware gin中间件](gin/middleware)
- [render gin返回数据封装](gin/render)
- [validator gin请求参数校验](gin/validator)
- [gobash 执行bash命令](gobash)
- [goFile 文件处理](goFile)
- [gohttp http客户端](gohttp)
- [jwt 鉴权](jwt)
- [krand 随机数和字符串生成器](krand)
- [logger 日志](logger)
- [mconf 配置处理](mconf)
- [mongo 客户端](mongo)
- [mysql 客户端](mysql)
- [nats 客户端](nats)
- [redis 客户端](redis)
- [snowFlake id生成器](snowFlake)

<br>

Expand Down
13 changes: 0 additions & 13 deletions errcode/module_code.go

This file was deleted.

File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions gin/errcode/module_code.go
@@ -0,0 +1,6 @@
package errcode

// 业务自定义错误
var (
// ErrorGetUserListFail = NewError(30000001, "获取用户数据失败")
)
84 changes: 57 additions & 27 deletions render/InOutLog.go → gin/middleware/InOutLog.go
@@ -1,4 +1,4 @@
package render
package middleware

import (
"bytes"
Expand All @@ -8,22 +8,59 @@ import (
"strings"
"time"

"pkg/logger"

"github.com/gin-gonic/gin"
"github.com/zhufuyi/pkg/logger"
)

var (
// 限制显示body内容最大长度
defaultMaxLength = 300

// 忽略打印路由
defaultIgnoreRoutes = map[string]struct{}{
"/ping": struct{}{},
"/pong": struct{}{},
}
)

// 忽略部分路由打印
var ignoreRoutes map[string]bool
func defaultOptions() *options {
return &options{
maxLength: defaultMaxLength,
ignoreRoutes: defaultIgnoreRoutes,
}
}

type options struct {
maxLength int
ignoreRoutes map[string]struct{}
}

// Option logger middleware options
type Option func(*options)

func init() {
ignoreRoutes = map[string]bool{
"/getSubID": true,
func (o *options) apply(opts ...Option) {
for _, opt := range opts {
opt(o)
}
}

// 限制显示body内容最大长度
const limitSize = 300
// WithMaxLen logger content max length
func WithMaxLen(maxLen int) Option {
return func(o *options) {
o.maxLength = maxLen
}
}

// WithIgnoreRoutes no logger content routes
func WithIgnoreRoutes(routes ...string) Option {
return func(o *options) {
for _, route := range routes {
o.ignoreRoutes[route] = struct{}{}
}
}
}

// ------------------------------------------------------------------------------------------

type bodyLogWriter struct {
gin.ResponseWriter
Expand All @@ -36,10 +73,13 @@ func (w bodyLogWriter) Write(b []byte) (int, error) {
}

// InOutLog gin输入输出日志
func InOutLog() gin.HandlerFunc {
func InOutLog(opts ...Option) gin.HandlerFunc {
o := defaultOptions()
o.apply(opts...)

return func(c *gin.Context) {
// 忽略打印指定的路由
if isIgnoreRoute(c.Request.URL.Path) {
if _, ok := o.ignoreRoutes[c.Request.URL.Path]; ok {
c.Next()
return
}
Expand All @@ -54,7 +94,7 @@ func InOutLog() gin.HandlerFunc {
logger.String("method", c.Request.Method),
logger.Any("url", c.Request.URL),
logger.Int("size", buf.Len()),
logger.String("body", getBodyData(&buf)),
logger.String("body", getBodyData(&buf, o.maxLength)),
)
} else {
logger.Info("<<<<<<",
Expand All @@ -78,30 +118,20 @@ func InOutLog() gin.HandlerFunc {
logger.String("url", c.Request.URL.Path),
logger.String("time", fmt.Sprintf("%dus", time.Now().Sub(start).Nanoseconds()/1000)),
logger.Int("size", newWriter.body.Len()),
logger.String("response", strings.TrimRight(getBodyData(newWriter.body), "\n")),
logger.String("response", strings.TrimRight(getBodyData(newWriter.body, o.maxLength), "\n")),
)
}
}

func getBodyData(buf *bytes.Buffer) string {
func getBodyData(buf *bytes.Buffer, maxLen int) string {
var body string

if buf.Len() > limitSize {
body = string(buf.Bytes()[:limitSize]) + " ...... "
if buf.Len() > maxLen {
body = string(buf.Bytes()[:maxLen]) + " ...... "
// 如果有敏感数据需要过滤掉,比如明文密码
} else {
body = buf.String()
}

return body
}

func isIgnoreRoute(routeValue string) bool {
for route, v := range ignoreRoutes {
if strings.Contains(routeValue, route) {
return v
}
}

return false
}

0 comments on commit df340d8

Please sign in to comment.