From 61fa7d26658fbcf9979cdd63173a0245abe5f8e9 Mon Sep 17 00:00:00 2001 From: yusing Date: Fri, 14 Nov 2025 10:58:55 +0800 Subject: [PATCH] chore(debug): add debug logging for bypass rules and remove for route validation --- internal/net/gphttp/middleware/bypass.go | 7 +++++++ internal/net/gphttp/middleware/middleware.go | 5 ++--- internal/route/route.go | 22 ++++++++++---------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/internal/net/gphttp/middleware/bypass.go b/internal/net/gphttp/middleware/bypass.go index 6b4801dd..85f618c0 100644 --- a/internal/net/gphttp/middleware/bypass.go +++ b/internal/net/gphttp/middleware/bypass.go @@ -4,6 +4,7 @@ import ( "net/http" "strings" + "github.com/rs/zerolog/log" "github.com/yusing/godoxy/internal/auth" "github.com/yusing/godoxy/internal/route/rules" ) @@ -13,6 +14,7 @@ type Bypass []rules.RuleOn func (b Bypass) ShouldBypass(w http.ResponseWriter, r *http.Request) bool { for _, rule := range b { if rule.Check(w, r) { + log.Debug().Str("rule_matched", rule.String()).Str("url", r.Host+r.URL.Path).Msg("bypassing request") return true } } @@ -20,6 +22,8 @@ func (b Bypass) ShouldBypass(w http.ResponseWriter, r *http.Request) bool { } type checkBypass struct { + name string + bypass Bypass modReq RequestModifier modRes ResponseModifier @@ -41,6 +45,7 @@ func (c *checkBypass) before(w http.ResponseWriter, r *http.Request) (proceedNex if c.modReq == nil || (!c.isEnforced(r) && c.bypass.ShouldBypass(w, r)) { return true } + log.Debug().Str("middleware", c.name).Str("url", r.Host+r.URL.Path).Msg("modifying request") return c.modReq.before(w, r) } @@ -48,6 +53,7 @@ func (c *checkBypass) modifyResponse(resp *http.Response) error { if c.modRes == nil || (!c.isEnforced(resp.Request) && c.bypass.ShouldBypass(rules.ResponseAsRW(resp), resp.Request)) { return nil } + log.Debug().Str("middleware", c.name).Str("url", resp.Request.Host+resp.Request.URL.Path).Msg("modifying response") return c.modRes.modifyResponse(resp) } @@ -56,6 +62,7 @@ func (m *Middleware) withCheckBypass() any { modReq, _ := m.impl.(RequestModifier) modRes, _ := m.impl.(ResponseModifier) return &checkBypass{ + name: m.Name(), bypass: m.Bypass, enforcedPathPrefixes: getEnforcedPathPrefixes(modReq, modRes), modReq: modReq, diff --git a/internal/net/gphttp/middleware/middleware.go b/internal/net/gphttp/middleware/middleware.go index efd7b118..26284245 100644 --- a/internal/net/gphttp/middleware/middleware.go +++ b/internal/net/gphttp/middleware/middleware.go @@ -5,7 +5,6 @@ import ( "net/http" "reflect" "sort" - "strings" "github.com/bytedance/sonic" "github.com/rs/zerolog" @@ -76,7 +75,7 @@ func NewMiddleware[ImplType any]() *Middleware { panic("MiddlewareFinalizer and MiddlewareFinalizerWithError are mutually exclusive") } return &Middleware{ - name: strings.ToLower(reflect.TypeFor[ImplType]().Name()), + name: reflect.TypeFor[ImplType]().Name(), construct: func() any { return new(ImplType) }, } } @@ -123,7 +122,7 @@ func (m *Middleware) finalize() error { func (m *Middleware) New(optsRaw OptionsRaw) (*Middleware, gperr.Error) { if m.construct == nil { // likely a middleware from compose if len(optsRaw) != 0 { - return nil, gperr.New("additional options not allowed for middleware ").Subject(m.name) + return nil, gperr.New("additional options not allowed for middleware").Subject(m.name) } return m, nil } diff --git a/internal/route/route.go b/internal/route/route.go index 007b0082..bc5767a8 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -122,24 +122,24 @@ func (r Routes) Contains(alias string) bool { } func (r *Route) Validate() gperr.Error { - pcs := make([]uintptr, 1) - runtime.Callers(2, pcs) - f := runtime.FuncForPC(pcs[0]) - fname := f.Name() + // pcs := make([]uintptr, 1) + // runtime.Callers(2, pcs) + // f := runtime.FuncForPC(pcs[0]) + // fname := f.Name() r.onceValidate.Do(func() { - filename, line := f.FileLine(pcs[0]) - if strings.HasPrefix(r.Alias, "godoxy") { - log.Debug().Str("route", r.Alias).Str("caller", fname).Str("file", filename).Int("line", line).Msg("validating route") - } + // filename, line := f.FileLine(pcs[0]) + // if strings.HasPrefix(r.Alias, "godoxy") { + // log.Debug().Str("route", r.Alias).Str("caller", fname).Str("file", filename).Int("line", line).Msg("validating route") + // } r.valErr.Set(r.validate()) }) return r.valErr.Get() } func (r *Route) validate() gperr.Error { - if strings.HasPrefix(r.Alias, "godoxy") { - log.Debug().Any("route", r).Msg("validating route") - } + // if strings.HasPrefix(r.Alias, "godoxy") { + // log.Debug().Any("route", r).Msg("validating route") + // } if r.Agent != "" { if r.Container != nil { return gperr.Errorf("specifying agent is not allowed for docker container routes")