Skip to content

Commit

Permalink
update: SkipPathList supports paths ending with a wildcard *
Browse files Browse the repository at this point in the history
  • Loading branch information
zodial committed Dec 5, 2024
1 parent 7998cfd commit 27db5a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
20 changes: 16 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import (
"io/ioutil"
"net/http"
"runtime/debug"
"strings"
"time"
)

// Telescope 启用望眼镜
func Telescope() gin.HandlerFunc {
return func(ctx *gin.Context) {
if b, ok := SkipPathList[ctx.FullPath()]; ok && b {
isSkip = true
isSkip = checkSkip(ctx.FullPath())
if isSkip {
ctx.Next()
return
} else {
isSkip = false
}
defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -55,3 +54,16 @@ func Telescope() gin.HandlerFunc {
ctx.Next()
}
}

func checkSkip(path string) bool {
for _, s := range SkipPathList {
if path == s {
return true
}
arr := strings.Split(s, "*")
if len(arr) > 1 && strings.Index(path, arr[0]) == 0 {
return true
}
}
return false
}
7 changes: 6 additions & 1 deletion telescope_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ type Providers struct {
}

var Routes = make(map[string]Type)
var SkipPathList = make(map[string]bool) //map里的路径不收集
var (
errorRecord bool //为true时,即使非debug模式也会开启望远镜,但只记录出错日志
hasError bool //标记错误日志,以便记录request为错误请求
isSkip bool //当为SkipPathList里的路径时,跳过所有收集
)

// SkipPathList Requests in the list are not logged. Supports paths ending with a wildcard `*`. For example:
// "/test" means only "/test" is not logged.
// "/test*" means "/test" and all requests with "/test" as a prefix are not logged.
// "/test/*" means all requests with "/test" as a prefix are not logged, but "/test" is logged.
var SkipPathList []string

// SetDB 任意框架下使用, 需要手动设置DB
func (t *Providers) SetDB(db *gorm.DB) {
t.Mysql = db
Expand Down

0 comments on commit 27db5a7

Please sign in to comment.