Router
type Router
type Router interface { MiddlewareRegistrar MiddlewareComposer ErrorHandlerRegistrar RouteRegistrar RouteMatcher // Add 注册请求处理器,返回对应的路由接口实例 Add([]string, string, HandlerFunc) (Route, error) // Remove 移除路由 Remove(methods []string, path string) error // Routes 返回注册的路由 Routes() []Route // URI 从处理器生成 URI。 URI(h HandlerFunc, params ...any) string // Reverse 从路由名称和提供的参数生成 URL。 Reverse(name string, params ...any) string}Router 是将请求路由到已注册路由的接口。
type RouteMatcher
type RouteMatcher interface { // Match 匹配路由 Match(r *http.Request, p *PathParams) RouteMatch}路由匹配器接口。
type RouterConfig
type RouterConfig struct { AllowOverwritingRoute bool UnescapePathParamValues bool UseEscapedPathForRouting bool RoutingTrailingSlash bool RouteCollector RouteCollector}type func NewRouter
func NewRouter(config RouterConfig) Routertype RouteCollector
type RouteCollector interface { MiddlewareRegistrar MiddlewareComposer ErrorHandlerRegistrar RouteRegistrar // Prefix 返回路由共用前缀 Prefix() string // Parent 返回上级路由收集器 Parent() RouteCollector // Router 返回所属路由器 Router() Router}路由收集器接口
func NewRouteCollector
func NewRouteCollector(prefix string, parent RouteCollector, router Router) RouteCollectortype RouteRegistrar
type RouteRegistrar interface { // Group 实现路由分组注册,实际调用 `RouteCollector.Route` 实现 Group(fn func(sub RouteCollector)) // Route 以指定前缀实现路由分组注册 Route(prefix string, fn func(sub RouteCollector)) // Some 为多个 HTTP 方法和路径注册新路由,并在路由器中匹配处理器。如果出错会 panic。 Some(methods []string, pattern string, h HandlerFunc) Route // Any 为所有支持的 HTTP 方法和路径注册新路由,并在路由器中匹配处理器。如果出错会 panic。 Any(pattern string, h HandlerFunc) Route // CONNECT 为路径注册新的 CONNECT 路由,并在路由器中匹配处理器。如果出错会 panic。 CONNECT(pattern string, h HandlerFunc) Route // DELETE 为路径注册新的 DELETE 路由,并在路由器中匹配处理器。如果出错会 panic。 DELETE(pattern string, h HandlerFunc) Route // GET 为路径注册新的 GET 路由,并在路由器中匹配处理器。如果出错会 panic。 GET(pattern string, h HandlerFunc) Route // HEAD 为路径注册新的 HEAD 路由,并在路由器中匹配处理器。如果出错会 panic。 HEAD(pattern string, h HandlerFunc) Route // OPTIONS 为路径注册新的 OPTIONS 路由,并在路由器中匹配处理器。如果出错会 panic。 OPTIONS(pattern string, h HandlerFunc) Route // PATCH 为路径注册新的 PATCH 路由,并在路由器中匹配处理器。如果出错会 panic。 PATCH(pattern string, h HandlerFunc) Route // POST 为路径注册新的 POST 路由,并在路由器中匹配处理器。如果出错会 panic。 POST(pattern string, h HandlerFunc) Route // PUT 为路径注册新的 PUT 路由,并在路由器中匹配处理器。如果出错会 panic。 PUT(pattern string, h HandlerFunc) Route // TRACE 为路径注册新的 TRACE 路由,并在路由器中匹配处理器。如果出错会 panic。 TRACE(pattern string, h HandlerFunc) Route // Handle 注册一个支持指定请求方法的路由 Handle(method, pattern string, h HandlerFunc) Route // Static 注册一个带有路径前缀的新路由,从提供的根目录提供静态文件。如果出错会 panic。 Static(prefix, root string) Route // File 注册一个带有路径的新路由以提供静态文件。如果出错会 panic。 File(pattern, file string) Route}路由注册表接口
type Route
type Route interface { MiddlewareRegistrar MiddlewareComposer // Router 返回所属路由器 Router() Router // Collector 返回所属收集器 Collector() RouteCollector // Name 返回路由名称 Name() string // SetName 设置路由名称 SetName(name string) Route // Title 返回路由标题 Title() string // SetTitle 设置路由标题 SetTitle(name string) Route // Pattern 路由路径表达式 Pattern() string // Methods 返回支持的 HTTP 请求方法 Methods() []string // Handler 返回注册的请求处理器函数 Handler() HandlerFunc // Params 返回支持的路由参数列表 Params() []string // RouteInfo 返回路由描述接口实现 RouteInfo() RouteInfo}路由接口。
type RouteInfo
type RouteInfo interface { // Router 返回所属路由器 Router() Router // Collector 返回所属收集器 Collector() RouteCollector // Name 返回路由名称 Name() string // Title 返回路由标题 Title() string // Methods 返回支持的请求方法列表 Methods() []string // Pattern 路由路径表达式 Pattern() string // Params 返回支持的路由参数列表 Params() []string // Reverse 通过提供的参数来反转路由表达式,返回为真实请求路径。 // 如果参数为空或 nil 时则尝试使用用默认值,若无法解决参数 // 则会 panic 错误 Reverse(params ...any) string // String 返回字符串形式 String() string}路由描述接口。
type RouteMatchType
type RouteMatchType uint8描述从路由角度来看请求可能处于的状态。
const ( // RouteMatchUnknown 是路由完成之前的状态。全新上下文的默认状态。 RouteMatchUnknown RouteMatchType = iota // RouteMatchNotFound 是路由器未找到当前请求匹配路由的状态 RouteMatchNotFound // RouteMatchMethodNotAllowed 是路由器未找到具有匹配路径 + 方法的路由的状态。 // 虽然路由器有匹配该路径的路由,但方法不同。 RouteMatchMethodNotAllowed // RouteMatchFound 是路由器找到路径 + 方法组合的精确匹配的状态 RouteMatchFound)type RouteMatch
type RouteMatch struct { // Type 包含 Router.Match 的枚举结果,有助于理解路由器是否真正匹配了路由, // 或者在处理器链的末尾我们有什么样的错误情况(404/405)。 Type RouteMatchType // AllowMethods 能够接受处理的请求方法列表,主要 // 在 Type 值为 RouteMatchMethodNotAllowed 时被使用。 AllowMethods []string // Handler 是路由器匹配的函数(链)。如果没有匹配,可能导致 ErrNotFound 或 ErrMethodNotAllowed。 Handler HandlerFunc // RouteInfo 是我们刚刚匹配的路由的信息 RouteInfo RouteInfo}RouteMatch 是 Router.Match 的结果对象。其主要目的是避免在路由器内部为 PathParams 分配内存。