Documentation ¶
Overview ¶
Package ruler implements a route manager based on the ruler.
Index ¶
- Variables
- type Matcher
- type Route
- type RouteBuilder
- func (b RouteBuilder) Clone() RouteBuilder
- func (b RouteBuilder) ContextHandler(h reqresp.Handler) RouteBuilder
- func (b RouteBuilder) ContextHandlerWithError(h reqresp.HandlerWithError) RouteBuilder
- func (b RouteBuilder) DELETE(handler http.Handler) RouteBuilder
- func (b RouteBuilder) DELETEContext(handler reqresp.Handler) RouteBuilder
- func (b RouteBuilder) DELETEContextWithError(handler reqresp.HandlerWithError) RouteBuilder
- func (b RouteBuilder) DELETEFunc(handler http.HandlerFunc) RouteBuilder
- func (b RouteBuilder) DebugProfiles() RouteBuilder
- func (b RouteBuilder) DebugRuleRoutes(router *Router) RouteBuilder
- func (b RouteBuilder) DebugVars() RouteBuilder
- func (b RouteBuilder) Desc(desc string) RouteBuilder
- func (b RouteBuilder) Extra(extra interface{}) RouteBuilder
- func (b RouteBuilder) GET(handler http.Handler) RouteBuilder
- func (b RouteBuilder) GETContext(handler reqresp.Handler) RouteBuilder
- func (b RouteBuilder) GETContextWithError(handler reqresp.HandlerWithError) RouteBuilder
- func (b RouteBuilder) GETFunc(handler http.HandlerFunc) RouteBuilder
- func (b RouteBuilder) Group(pathPrefix string) RouteBuilder
- func (b RouteBuilder) HEAD(handler http.Handler) RouteBuilder
- func (b RouteBuilder) HEADContext(handler reqresp.Handler) RouteBuilder
- func (b RouteBuilder) HEADContextWithError(handler reqresp.HandlerWithError) RouteBuilder
- func (b RouteBuilder) HEADFunc(handler http.HandlerFunc) RouteBuilder
- func (b RouteBuilder) Handler(handler http.Handler) RouteBuilder
- func (b RouteBuilder) HandlerFunc(handler http.HandlerFunc) RouteBuilder
- func (b RouteBuilder) Header(key, value string) RouteBuilder
- func (b RouteBuilder) Host(host string) RouteBuilder
- func (b RouteBuilder) Method(method string) RouteBuilder
- func (b RouteBuilder) OPTIONS(handler http.Handler) RouteBuilder
- func (b RouteBuilder) OPTIONSContext(handler reqresp.Handler) RouteBuilder
- func (b RouteBuilder) OPTIONSContextWithError(handler reqresp.HandlerWithError) RouteBuilder
- func (b RouteBuilder) OPTIONSFunc(handler http.HandlerFunc) RouteBuilder
- func (b RouteBuilder) PATCH(handler http.Handler) RouteBuilder
- func (b RouteBuilder) PATCHContext(handler reqresp.Handler) RouteBuilder
- func (b RouteBuilder) PATCHContextWithError(handler reqresp.HandlerWithError) RouteBuilder
- func (b RouteBuilder) PATCHFunc(handler http.HandlerFunc) RouteBuilder
- func (b RouteBuilder) POST(handler http.Handler) RouteBuilder
- func (b RouteBuilder) POSTContext(handler reqresp.Handler) RouteBuilder
- func (b RouteBuilder) POSTContextWithError(handler reqresp.HandlerWithError) RouteBuilder
- func (b RouteBuilder) POSTFunc(handler http.HandlerFunc) RouteBuilder
- func (b RouteBuilder) PUT(handler http.Handler) RouteBuilder
- func (b RouteBuilder) PUTContext(handler reqresp.Handler) RouteBuilder
- func (b RouteBuilder) PUTContextWithError(handler reqresp.HandlerWithError) RouteBuilder
- func (b RouteBuilder) PUTFunc(handler http.HandlerFunc) RouteBuilder
- func (b RouteBuilder) Path(path string) RouteBuilder
- func (b RouteBuilder) PathPrefix(pathPrefix string) RouteBuilder
- func (b RouteBuilder) Prefix() string
- func (b RouteBuilder) Priority(priority int) RouteBuilder
- func (b RouteBuilder) Query(key, value string) RouteBuilder
- func (b RouteBuilder) Use(middlewares ...middleware.Middleware) RouteBuilder
- func (b RouteBuilder) UseFunc(middlewares ...middleware.MiddlewareFunc) RouteBuilder
- func (b RouteBuilder) WrapRegister(wrap func(register func(Route), route Route)) RouteBuilder
- type Router
- func (r *Router) Group(pathPrefix string) RouteBuilder
- func (r *Router) Host(host string) RouteBuilder
- func (r *Router) Path(path string) RouteBuilder
- func (r *Router) PathPrefix(pathPrefix string) RouteBuilder
- func (r *Router) Register(route Route)
- func (r *Router) RouteBuilder() RouteBuilder
- func (r *Router) Routes() (routes []Route)
- func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
Constants ¶
This section is empty.
Variables ¶
var DefaultRouter = NewRouter()
DefaultRouter is the default router.
Functions ¶
This section is empty.
Types ¶
type Route ¶
type Route struct { // Priority is the priority of the route. // // The bigger the value, the higher the priority. Priority int `json:"priority" yaml:"priority" xml:"priority"` // Matcher is used to match the request. Matcher Matcher `json:"-"` // Handler is the handler of the route. Handler http.Handler `json:"-"` // Extra is the extra data of the route. Extra any `json:"extra,omitempty" yaml:"extra,omitempty" xml:"extra,omitempty"` // Desc is the description of the route, which may be matcher string. Desc string `json:"desc,omitempty" yaml:"desc,omitempty" xml:"desc,omitempty"` // contains filtered or unexported fields }
Route is a http request route.
func (*Route) ServeHTTP ¶
func (r *Route) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP implements the interface http.Handler.
func (*Route) Use ¶
func (r *Route) Use(ms ...middleware.Middleware)
Use applies the middlewares on the route handler.
type RouteBuilder ¶
type RouteBuilder struct {
// contains filtered or unexported fields
}
RouteBuilder is used to build the route.
func NewRouteBuilder ¶
func NewRouteBuilder(register func(Route)) RouteBuilder
NewRouteBuilder returns a new route builder.
func (RouteBuilder) Clone ¶ added in v0.36.2
func (b RouteBuilder) Clone() RouteBuilder
Clone clones itself and returns a new route builder.
func (RouteBuilder) ContextHandler ¶
func (b RouteBuilder) ContextHandler(h reqresp.Handler) RouteBuilder
ContextHandler is the same HandlerFunc, but wraps the request and response into Context.
func (RouteBuilder) ContextHandlerWithError ¶
func (b RouteBuilder) ContextHandlerWithError(h reqresp.HandlerWithError) RouteBuilder
ContextHandlerWithError is the same ContextHandler, but supports to return an error.
func (RouteBuilder) DELETE ¶
func (b RouteBuilder) DELETE(handler http.Handler) RouteBuilder
DELETE is a convenient function to register the route with the handler, which is the same as b.Method(http.MethodDelete).Handler(handler).
func (RouteBuilder) DELETEContext ¶
func (b RouteBuilder) DELETEContext(handler reqresp.Handler) RouteBuilder
DELETEContext is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodDelete).Handler(handler).
func (RouteBuilder) DELETEContextWithError ¶
func (b RouteBuilder) DELETEContextWithError(handler reqresp.HandlerWithError) RouteBuilder
DELETEContextWithError is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodDelete).Handler(handler).
func (RouteBuilder) DELETEFunc ¶
func (b RouteBuilder) DELETEFunc(handler http.HandlerFunc) RouteBuilder
DELETEFunc is a convenient function to register the route with the function handler, which is the same as b.Method(http.MethodDelete).Handler(handler).
func (RouteBuilder) DebugProfiles ¶ added in v0.42.0
func (b RouteBuilder) DebugProfiles() RouteBuilder
DebugProfiles registers the pprof routes with the path prefix "/debug/pprof/".
func (RouteBuilder) DebugRuleRoutes ¶ added in v0.42.0
func (b RouteBuilder) DebugRuleRoutes(router *Router) RouteBuilder
DebugRuleRoutes registers the rule-routes route with the path "/debug/router/rule/routes".
func (RouteBuilder) DebugVars ¶ added in v0.42.0
func (b RouteBuilder) DebugVars() RouteBuilder
DebugVars registers the vars route with the path "/debug/vars".
func (RouteBuilder) Desc ¶ added in v0.42.0
func (b RouteBuilder) Desc(desc string) RouteBuilder
Desc sets the description of the route.
func (RouteBuilder) Extra ¶ added in v0.34.0
func (b RouteBuilder) Extra(extra interface{}) RouteBuilder
Extra sets the extra data of the route.
func (RouteBuilder) GET ¶
func (b RouteBuilder) GET(handler http.Handler) RouteBuilder
GET is a convenient function to register the route with the handler, which is the same as b.Method(http.MethodGet).Handler(handler).
func (RouteBuilder) GETContext ¶
func (b RouteBuilder) GETContext(handler reqresp.Handler) RouteBuilder
GETContext is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodGet).Handler(handler).
func (RouteBuilder) GETContextWithError ¶
func (b RouteBuilder) GETContextWithError(handler reqresp.HandlerWithError) RouteBuilder
GETContextWithError is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodGet).Handler(handler).
func (RouteBuilder) GETFunc ¶
func (b RouteBuilder) GETFunc(handler http.HandlerFunc) RouteBuilder
GETFunc is a convenient function to register the route with the function handler, which is the same as b.Method(http.MethodGet).Handler(handler).
func (RouteBuilder) Group ¶
func (b RouteBuilder) Group(pathPrefix string) RouteBuilder
Group appends the prefix of the paths of a group of routes when they are registered,
func (RouteBuilder) HEAD ¶
func (b RouteBuilder) HEAD(handler http.Handler) RouteBuilder
HEAD is a convenient function to register the route with the handler, which is the same as b.Method(http.MethodHead).Handler(handler).
func (RouteBuilder) HEADContext ¶
func (b RouteBuilder) HEADContext(handler reqresp.Handler) RouteBuilder
HEADContext is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodHead).Handler(handler).
func (RouteBuilder) HEADContextWithError ¶
func (b RouteBuilder) HEADContextWithError(handler reqresp.HandlerWithError) RouteBuilder
HEADContextWithError is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodHead).Handler(handler).
func (RouteBuilder) HEADFunc ¶
func (b RouteBuilder) HEADFunc(handler http.HandlerFunc) RouteBuilder
HEADFunc is a convenient function to register the route with the function handler, which is the same as b.Method(http.MethodHead).Handler(handler).
func (RouteBuilder) Handler ¶
func (b RouteBuilder) Handler(handler http.Handler) RouteBuilder
Handler registers the route with the handler.
func (RouteBuilder) HandlerFunc ¶
func (b RouteBuilder) HandlerFunc(handler http.HandlerFunc) RouteBuilder
HandlerFunc registers the route with the handler functions.
func (RouteBuilder) Header ¶
func (b RouteBuilder) Header(key, value string) RouteBuilder
Header adds the header key-value match ruler.
func (RouteBuilder) Host ¶
func (b RouteBuilder) Host(host string) RouteBuilder
Host adds the host match ruler.
func (RouteBuilder) Method ¶
func (b RouteBuilder) Method(method string) RouteBuilder
Method adds the method match ruler.
func (RouteBuilder) OPTIONS ¶ added in v0.42.0
func (b RouteBuilder) OPTIONS(handler http.Handler) RouteBuilder
OPTIONS is a convenient function to register the route with the handler, which is the same as b.Method(http.MethodOptions).Handler(handler).
func (RouteBuilder) OPTIONSContext ¶ added in v0.42.0
func (b RouteBuilder) OPTIONSContext(handler reqresp.Handler) RouteBuilder
OPTIONSContext is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodOptions).Handler(handler).
func (RouteBuilder) OPTIONSContextWithError ¶ added in v0.42.0
func (b RouteBuilder) OPTIONSContextWithError(handler reqresp.HandlerWithError) RouteBuilder
OPTIONSContextWithError is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodOptions).Handler(handler).
func (RouteBuilder) OPTIONSFunc ¶ added in v0.42.0
func (b RouteBuilder) OPTIONSFunc(handler http.HandlerFunc) RouteBuilder
OPTIONS is a convenient function to register the route with the handler, which is the same as b.Method(http.MethodOptions).Handler(handler).
func (RouteBuilder) PATCH ¶
func (b RouteBuilder) PATCH(handler http.Handler) RouteBuilder
PATCH is a convenient function to register the route with the handler, which is the same as b.Method(http.MethodPatch).Handler(handler).
func (RouteBuilder) PATCHContext ¶
func (b RouteBuilder) PATCHContext(handler reqresp.Handler) RouteBuilder
PATCHContext is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodPatch).Handler(handler).
func (RouteBuilder) PATCHContextWithError ¶
func (b RouteBuilder) PATCHContextWithError(handler reqresp.HandlerWithError) RouteBuilder
PATCHContextWithError is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodPatch).Handler(handler).
func (RouteBuilder) PATCHFunc ¶
func (b RouteBuilder) PATCHFunc(handler http.HandlerFunc) RouteBuilder
PATCHFunc is a convenient function to register the route with the function handler, which is the same as b.Method(http.MethodPatch).Handler(handler).
func (RouteBuilder) POST ¶
func (b RouteBuilder) POST(handler http.Handler) RouteBuilder
POST is a convenient function to register the route with the handler, which is the same as b.Method(http.MethodPost).Handler(handler).
func (RouteBuilder) POSTContext ¶
func (b RouteBuilder) POSTContext(handler reqresp.Handler) RouteBuilder
POSTContext is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodPost).Handler(handler).
func (RouteBuilder) POSTContextWithError ¶
func (b RouteBuilder) POSTContextWithError(handler reqresp.HandlerWithError) RouteBuilder
POSTContextWithError is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodPost).Handler(handler).
func (RouteBuilder) POSTFunc ¶
func (b RouteBuilder) POSTFunc(handler http.HandlerFunc) RouteBuilder
POSTFunc is a convenient function to register the route with the function handler, which is the same as b.Method(http.MethodPost).Handler(handler).
func (RouteBuilder) PUT ¶
func (b RouteBuilder) PUT(handler http.Handler) RouteBuilder
PUT is a convenient function to register the route with the handler, which is the same as b.Method(http.MethodPut).Handler(handler).
func (RouteBuilder) PUTContext ¶
func (b RouteBuilder) PUTContext(handler reqresp.Handler) RouteBuilder
PUTContext is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodPut).Handler(handler).
func (RouteBuilder) PUTContextWithError ¶
func (b RouteBuilder) PUTContextWithError(handler reqresp.HandlerWithError) RouteBuilder
PUTContextWithError is a convenient function to register the route with the context handler, which is the same as b.Method(http.MethodPut).Handler(handler).
func (RouteBuilder) PUTFunc ¶
func (b RouteBuilder) PUTFunc(handler http.HandlerFunc) RouteBuilder
PUTFunc is a convenient function to register the route with the function handler, which is the same as b.Method(http.MethodPut).Handler(handler).
func (RouteBuilder) Path ¶
func (b RouteBuilder) Path(path string) RouteBuilder
Path adds the path match rule, which ignores the trailling "/".
- If the group is set, it will add it into path as the prefix.
- It supports the path parameters, such as "/prefix/{param1}/path/{param2}/to", and put the parsed parameter values into the Data field if a *reqresp.Context can be got from *http.Request.
func (RouteBuilder) PathPrefix ¶
func (b RouteBuilder) PathPrefix(pathPrefix string) RouteBuilder
PathPrefix adds the path prefeix match rule, which ignores the trailling "/".
- If the group is set, it will add it into pathPrefix as the prefix.
- It supports the path parameters, such as "/prefix/{param1}/path/{param2}/to", and put the parsed parameter values into the Data field if a *reqresp.Context can be got from *http.Request.
func (RouteBuilder) Prefix ¶ added in v0.43.0
func (b RouteBuilder) Prefix() string
Prefix returns the group path prefix, which is not the path prefix matcher.
func (RouteBuilder) Priority ¶
func (b RouteBuilder) Priority(priority int) RouteBuilder
Priority sets the priority the route.
func (RouteBuilder) Query ¶
func (b RouteBuilder) Query(key, value string) RouteBuilder
Query adds the query key-value match ruler.
func (RouteBuilder) Use ¶
func (b RouteBuilder) Use(middlewares ...middleware.Middleware) RouteBuilder
Use appends the http handler middlewares that act on the later handler.
func (RouteBuilder) UseFunc ¶ added in v0.43.0
func (b RouteBuilder) UseFunc(middlewares ...middleware.MiddlewareFunc) RouteBuilder
UseFunc appends the http handler function middlewares that act on the later handler.
func (RouteBuilder) WrapRegister ¶ added in v0.45.0
func (b RouteBuilder) WrapRegister(wrap func(register func(Route), route Route)) RouteBuilder
WrapRegister wraps the register and returns a new route builder.
type Router ¶
type Router struct { // NotFound is used when the manager is used as http.Handler // and does not find the route. // // Default: handler.Handler404 NotFound http.Handler // Middlewares is used to manage the middlewares and applied to each route // when registering it. So, the middlewares will be run after routing // and never be run if not found the route. Middlewares *middleware.Manager // contains filtered or unexported fields }
Router is used to manage a set of routes based on the ruler.
func (*Router) Group ¶
func (r *Router) Group(pathPrefix string) RouteBuilder
Group returns a route builder with the prefix path group, which will register the built route into the router.
func (*Router) Host ¶
func (r *Router) Host(host string) RouteBuilder
Host returns a route builder with the host matcher, which will register the built route into the router.
func (*Router) Path ¶
func (r *Router) Path(path string) RouteBuilder
Path returns a route builder with the path matcher, which will register the built route into the router.
func (*Router) PathPrefix ¶
func (r *Router) PathPrefix(pathPrefix string) RouteBuilder
PathPrefix returns a route builder with the path prefix matcher, which will register the built route into the router.
func (*Router) Register ¶ added in v0.42.0
Register registers the route.
NOTICE: if both routes match a request, the handler of the higher priority route will be executed, and that of the lower never be executed.
func (*Router) RouteBuilder ¶ added in v0.42.0
func (r *Router) RouteBuilder() RouteBuilder
Route returns a new route builder.