NewCompositeBackend 创建 CompositeBackend 实例
(defaultBackend BackendProtocol, routes []RouteConfig)
| 23 | |
| 24 | // NewCompositeBackend 创建 CompositeBackend 实例 |
| 25 | func NewCompositeBackend(defaultBackend BackendProtocol, routes []RouteConfig) *CompositeBackend { |
| 26 | // 按前缀长度降序排序,确保优先匹配最长前缀 |
| 27 | sortedRoutes := make([]RouteConfig, len(routes)) |
| 28 | copy(sortedRoutes, routes) |
| 29 | sort.Slice(sortedRoutes, func(i, j int) bool { |
| 30 | return len(sortedRoutes[i].Prefix) > len(sortedRoutes[j].Prefix) |
| 31 | }) |
| 32 | |
| 33 | return &CompositeBackend{ |
| 34 | defaultBackend: defaultBackend, |
| 35 | routes: sortedRoutes, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // selectBackend 根据路径选择后端 |
| 40 | // 返回后端和剥离前缀后的路径 |