fix(rules): routes without default rule panics

This commit is contained in:
yusing 2025-06-16 21:36:20 +08:00
parent 4189ffa1db
commit 9d712b91ff

View file

@ -54,7 +54,13 @@ type (
// if no rule matches and default rule is not set,
// the request is passed to the upstream.
func (rules Rules) BuildHandler(caller string, up http.Handler) http.HandlerFunc {
var defaultRule *Rule
defaultRule := &Rule{
Name: "default",
Do: Command{
raw: "pass",
exec: BypassCommand{},
},
}
nonDefaultRules := make(Rules, 0, len(rules))
for i, rule := range rules {
@ -79,6 +85,10 @@ func (rules Rules) BuildHandler(caller string, up http.Handler) http.HandlerFunc
}
}
if len(nonDefaultRules) == 0 {
nonDefaultRules = rules
}
return func(w http.ResponseWriter, r *http.Request) {
cache := NewCache()
defer cache.Release()