From 27db5a7fb1a5c9af99159e97a9fbc723436c0e7b Mon Sep 17 00:00:00 2001 From: zodial Date: Thu, 5 Dec 2024 11:18:28 +0800 Subject: [PATCH] update: SkipPathList supports paths ending with a wildcard `*` --- middleware.go | 20 ++++++++++++++++---- telescope_provider.go | 7 ++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/middleware.go b/middleware.go index 96f6f98..838c2f2 100644 --- a/middleware.go +++ b/middleware.go @@ -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 { @@ -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 +} diff --git a/telescope_provider.go b/telescope_provider.go index 9633a22..ace6163 100644 --- a/telescope_provider.go +++ b/telescope_provider.go @@ -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