chore(debug): add debug logging for bypass rules and remove for route validation

This commit is contained in:
yusing
2025-11-14 10:58:55 +08:00
parent 615521ee1c
commit 61fa7d2665
3 changed files with 20 additions and 14 deletions

View File

@@ -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,

View File

@@ -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
}

View File

@@ -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")