ruler

package
v0.49.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2024 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Overview

Package ruler implements a route manager based on the ruler.

Index

Constants

This section is empty.

Variables

View Source
var DefaultRouter = NewRouter()

DefaultRouter is the default router.

Functions

This section is empty.

Types

type Matcher added in v0.42.0

type Matcher interface {
	Match(*http.Request) bool
}

Matcher is used to check whether the route matches the request.

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 NewRoute

func NewRoute(priority int, matcher Matcher, handler http.Handler) Route

NewRoute returns a new 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.

func (Route) WithDesc added in v0.49.0

func (r Route) WithDesc(desc string) Route

WithDesc returns a new Route with the desc.

func (Route) WithExtra added in v0.49.0

func (r Route) WithExtra(extra any) Route

WithExtra returns a new Route with the extra.

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 NewRouter

func NewRouter() *Router

NewRouter returns a new router.

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

func (r *Router) Register(route Route)

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.

func (*Router) Routes added in v0.42.0

func (r *Router) Routes() (routes []Route)

Routes returns all the registered routes, which must be read-only.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP implements the interface http.Handler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL