Files
crossbar/internal/config/route.go
T
kyle 4e1dd03d07 Route templates: a route named x-* serves any request route x-<something>
Implemented-By: OpenCode session (model recorded in docs/implementer-log.md)
2026-09-25 13:38:24 -07:00

41 lines
1.2 KiB
Go

package config
import (
"regexp"
"strings"
)
// templateName matches a route template: a valid route name ending in "-*".
var templateName = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*-\*$`)
// Route resolves a request route name: an exact entry wins; else the longest template
// "<prefix>-*" whose prefix (including the dash) starts name with a non-empty remainder;
// else ok is false. key is the config key that matched (the template's name for a template).
// A name that is not a valid route name (the pattern below) or contains '*' never matches.
func (c *Config) Route(name string) (r Route, key string, ok bool) {
if !routeName.MatchString(name) {
return Route{}, "", false
}
if rt, found := c.Routes[name]; found {
return rt, name, true
}
var (
best Route
bestKey string
bestLen int
)
for tmpl, rt := range c.Routes {
if !templateName.MatchString(tmpl) {
continue
}
prefix := tmpl[:len(tmpl)-1] // drop the trailing '*', keeping the dash
if len(prefix) > bestLen && strings.HasPrefix(name, prefix) && len(name) > len(prefix) {
best, bestKey, bestLen = rt, tmpl, len(prefix)
}
}
if bestKey == "" {
return Route{}, "", false
}
return best, bestKey, true
}