Documentation
¶
Overview ¶
Package routers provides the router interface and the implementation of different routing policies.
Index ¶
- Variables
- func BackendPoolPipelineNamePrefix(serverName string) string
- func GenerateBackendPoolPipeline(serverName string, ruleIndex, pathIndex int) string
- func Register(k *Kind)
- type Header
- type Headers
- type Host
- type Kind
- type MethodType
- type Params
- type Path
- func (p *Path) AllowIP(ip string) bool
- func (p *Path) GetBackend() string
- func (p *Path) GetClientMaxBodySize() int64
- func (p *Path) GetExactPath() string
- func (p *Path) GetPathPrefix() string
- func (p *Path) GetPathRegexp() string
- func (p *Path) Init(parentIPFilter *ipfilter.IPFilter, serverName string, ruleIndex, pathIndex int)
- func (p *Path) Match(context *RouteContext) (result bool)
- func (p *Path) Validate() error
- type Paths
- type Queries
- type Query
- type Route
- type RouteContext
- type Router
- type Rule
- type Rules
Constants ¶
This section is empty.
Variables ¶
var ( // MALL represents the methodType that can match all methods. MALL = mCONNECT | mDELETE | mGET | mHEAD | mOPTIONS | mPATCH | mPOST | mPUT | mTRACE // Methods represents the mapping of http method and methodType. Methods = map[string]MethodType{ http.MethodGet: mGET, http.MethodHead: mHEAD, http.MethodPost: mPOST, http.MethodPut: mPUT, http.MethodPatch: mPATCH, http.MethodDelete: mDELETE, http.MethodConnect: mCONNECT, http.MethodOptions: mOPTIONS, http.MethodTrace: mTRACE, } )
Functions ¶
func BackendPoolPipelineNamePrefix ¶ added in v2.10.0
func GenerateBackendPoolPipeline ¶ added in v2.10.0
Types ¶
type Header ¶
type Header struct {
Key string `json:"key" jsonschema:"required"`
Values []string `json:"values,omitempty" jsonschema:"uniqueItems=true"`
Regexp string `json:"regexp,omitempty" jsonschema:"format=regexp"`
// contains filtered or unexported fields
}
Header is the third level entry of router. A header entry is always under a specific path entry, that is to mean the headers entry will only be checked after a path entry matched. However, the headers entry has a higher priority than the path entry itself.
type Headers ¶
type Headers []*Header
Headers represents the set of headers.
type Host ¶
type Host struct {
IsRegexp bool `json:"isRegexp,omitempty"`
Value string `json:"value" jsonschema:"required"`
// contains filtered or unexported fields
}
Host defines the host match rule.
type Kind ¶
type Kind struct {
// Name is the name of the router kind.
Name string
// Description is the description of the router.
Description string
// CreateInstance creates a new router instance of the kind.
CreateInstance func(rules Rules) Router
}
Kind contains the meta data and functions of a router kind.
type MethodType ¶
type MethodType uint
MethodType represents the bit-operated representation of the http method.
type Params ¶
type Params struct {
Keys, Values []string
}
Params are used to store the variables in the search path and their corresponding values.
type Path ¶
type Path struct {
IPFilterSpec *ipfilter.Spec `json:"ipFilter,omitempty"`
Path string `json:"path,omitempty" jsonschema:"pattern=^/"`
PathPrefix string `json:"pathPrefix,omitempty" jsonschema:"pattern=^/"`
PathRegexp string `json:"pathRegexp,omitempty" jsonschema:"format=regexp"`
RewriteTarget string `json:"rewriteTarget,omitempty"`
Methods []string `json:"methods,omitempty" jsonschema:"uniqueItems=true,format=httpmethod-array"`
Backend string `json:"backend,omitempty"`
BackendPool *httpproxy.ServerPoolSpec `json:"backendPool,omitempty"`
ClientMaxBodySize int64 `json:"clientMaxBodySize,omitempty"`
Headers Headers `json:"headers,omitempty"`
Queries Queries `json:"queries,omitempty"`
MatchAllHeader bool `json:"matchAllHeader,omitempty"`
MatchAllQuery bool `json:"matchAllQuery,omitempty"`
SetHeaders map[string]string `json:"setHeaders,omitempty"`
// contains filtered or unexported fields
}
Path is second level entry of router.
func (*Path) GetBackend ¶
GetBackend is used to get the backend corresponding to the route.
func (*Path) GetClientMaxBodySize ¶
GetClientMaxBodySize is used to get the clientMaxBodySize corresponding to the route.
func (*Path) GetExactPath ¶ added in v2.6.3
GetExactPath returns the exact path of the route.
func (*Path) GetPathPrefix ¶ added in v2.6.3
GetPathPrefix returns the path prefix of the route.
func (*Path) GetPathRegexp ¶ added in v2.6.3
GetPathRegexp returns the path regexp of the route.
func (*Path) Match ¶
func (p *Path) Match(context *RouteContext) (result bool)
Match is the matching function of path.
type Queries ¶
type Queries []*Query
Queries represents the set of queries.
type Query ¶
type Query struct {
Key string `json:"key" jsonschema:"required"`
Values []string `json:"values,omitempty" jsonschema:"uniqueItems=true"`
Regexp string `json:"regexp,omitempty" jsonschema:"format=regexp"`
// contains filtered or unexported fields
}
Query is the third level entry.
type Route ¶
type Route interface {
protocols.Route
// Rewrite for path rewriting.
Rewrite(context *RouteContext)
// GetBackend is used to get the backend corresponding to the route.
GetBackend() string
// GetClientMaxBodySize is used to get the clientMaxBodySize corresponding to the route.
GetClientMaxBodySize() int64
// GetExactPath is used to get the exact path corresponding to the route.
GetExactPath() string
// GetPathPrefix is used to get the path prefix corresponding to the route.
GetPathPrefix() string
// GetPathRegexp is used to get the path regexp corresponding to the route.
GetPathRegexp() string
}
Route is the corresponding route interface for different routing policies.
type RouteContext ¶
type RouteContext struct {
// Request is httpprot.Request.
Request *httpprot.Request
// Path represents the path of the request.
Path string
// Method represents the MethodType corresponding to the http method.
Method MethodType
// Params are used to store the variables in the search path and their corresponding values.
Params Params
// Cacheable means whether the route can be cached or not.
Cacheable bool
// Route represents the results of this search
Route Route
HeaderMismatch, MethodMismatch, QueryMismatch, IPMismatch bool
// contains filtered or unexported fields
}
RouteContext is the context container in the route search
func NewContext ¶
func NewContext(req *httpprot.Request) *RouteContext
NewContext creates a context instance.
func (*RouteContext) GetCaptures ¶
func (ctx *RouteContext) GetCaptures() map[string]string
GetCaptures is used to get and cache path parameter mappings.
func (*RouteContext) GetHeader ¶
func (ctx *RouteContext) GetHeader() http.Header
GetHeader is used to get request http header.
func (*RouteContext) GetHost ¶
func (ctx *RouteContext) GetHost() string
GetHost is used to get and cache host.
func (*RouteContext) GetQueries ¶
func (ctx *RouteContext) GetQueries() url.Values
GetQueries is used to get and cache query params.
func (*RouteContext) ParseNginxLikeVar ¶ added in v2.10.0
func (ctx *RouteContext) ParseNginxLikeVar(val string) string
ParseNginxLikeVar parses and substitutes nginx-like variables in the given string.
Supported variable syntaxes:
- $var - Simple variable reference
- ${var} - Braced variable reference
- ${var:default} - Variable with default value
Examples:
- "$host" -> "example.com"
- "${host}-backend" -> "example.com-backend"
- "${remote_user:anonymous}" -> "anonymous" (if remote_user is empty)
- "$scheme://$host$uri" -> "http://example.com/path"
- "$proxy_add_x_forwarded_for" -> "10.0.0.1, 192.168.1.100" (appends client IP to XFF)
Supported variables include request info ($uri, $request_method, $args), headers ($http_*), query params ($arg_*), cookies ($cookie_*), proxy variables ($proxy_add_x_forwarded_for), and more.
type Router ¶
type Router interface {
// Search performs a route search and populates the context with search-related information.
Search(context *RouteContext)
}
Router is the interface for route search.
type Rule ¶
type Rule struct {
// NOTICE: If the field is a pointer, it must have `omitempty` in tag `json`
// when it has `omitempty` in tag `jsonschema`.
// Otherwise it will output null value, which is invalid in json schema (the type is object).
// the original reason is the jsonscheme(genjs) has not support multiple types.
// Reference: https://github.com/alecthomas/jsonschema/issues/30
// In the future if we have the scenario where we need marshal the field, but omitempty
// in the schema, we are suppose to support multiple types on our own.
IPFilterSpec *ipfilter.Spec `json:"ipFilter,omitempty"`
Host string `json:"host,omitempty"`
HostRegexp string `json:"hostRegexp,omitempty" jsonschema:"format=regexp"`
Hosts []Host `json:"hosts,omitempty"`
Paths Paths `json:"paths,omitempty"`
// contains filtered or unexported fields
}
Rule is first level entry of router.
func (*Rule) MatchHost ¶
func (rule *Rule) MatchHost(ctx *RouteContext) bool
MatchHost matches the host of the request to the rule.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package ordered provides the router implementation of ordered routing policy.
|
Package ordered provides the router implementation of ordered routing policy. |
|
Package radixtree provides the router implementation of radix tree routing policy.
|
Package radixtree provides the router implementation of radix tree routing policy. |