selectBackend 根据路径选择后端 返回后端和剥离前缀后的路径
(path string)
| 39 | // selectBackend 根据路径选择后端 |
| 40 | // 返回后端和剥离前缀后的路径 |
| 41 | func (b *CompositeBackend) selectBackend(path string) (BackendProtocol, string) { |
| 42 | for _, route := range b.routes { |
| 43 | if strippedPath, found := strings.CutPrefix(path, route.Prefix); found { |
| 44 | // 确保路径以 / 开头 |
| 45 | if strippedPath == "" { |
| 46 | strippedPath = "/" |
| 47 | } else if !strings.HasPrefix(strippedPath, "/") { |
| 48 | strippedPath = "/" + strippedPath |
| 49 | } |
| 50 | return route.Backend, strippedPath |
| 51 | } |
| 52 | } |
| 53 | return b.defaultBackend, path |
| 54 | } |
| 55 | |
| 56 | // backendWithPrefix 后端及其路由前缀 |
| 57 | type backendWithPrefix struct { |