Documentation ¶
Overview ¶
Package router provides an entrypoint router for the api gateway.
Index ¶
- Variables
- type Matcher
- type MatcherFunc
- type Route
- type Router
- func (r *Router) AddRoutes(routes ...Route)
- func (r *Router) DelRoutes(routes ...Route)
- func (r *Router) DelRoutesByIds(ids ...string)
- func (r *Router) GetRoute(id string) (Route, bool)
- func (r *Router) Handle(c *core.Context) (handled bool)
- func (r *Router) Routes() []Route
- func (r *Router) Serve(c *core.Context)
- func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (r *Router) Use(mws ...middleware.Middleware)
Constants ¶
This section is empty.
Variables ¶
var ( // AlwaysTrue is a route matcher that always reutrns true. AlwaysTrue = MatcherFunc(func(*http.Request) bool { return true }) // AlwaysFalse is a route matcher that always reutrns false. AlwaysFalse = MatcherFunc(func(*http.Request) bool { return false }) )
var AfterRoute core.Handler = upstream.Forward
AfterRoute is the next handler after the route.
var DefaultRouter = New()
DefaultRouter is the default global http router.
Functions ¶
This section is empty.
Types ¶
type MatcherFunc ¶
MatcherFunc is a route matcher function.
type Route ¶
type Route struct { // Required RouteId string `json:"routeId" yaml:"routeId"` UpstreamId string `json:"upstreamId" yaml:"upstreamId"` // Optional // // The bigger the value, the higher the priority. Priority int `json:"priority,omitempty" yaml:"priority,omitempty"` // Optional // // If true, the route is called in the apigateway inside, and not routed. Protect bool `json:"protect,omitempty" yaml:"protect,omitempty"` // Optional RequestTimeout time.Duration `json:"requestTimeout,omitempty" yaml:"requestTimeout,omitempty"` ForwardTimeout time.Duration `json:"forwardTimeout,omitempty" yaml:"forwardTimeout,omitempty"` // Optional // // The original configuration of the route. Config any `json:"config,omitempty" yaml:"config,omitempty"` // Optional // // Extra is the extra route information. Extra any `json:"-" yaml:"-"` // Optional // // It may be the description of the matcher. Desc string `json:"desc" yaml:"desc"` Matcher `json:"-" yaml:"-"` // Required core.Handler `json:"-" yaml:"-"` // Optional, Default: AfterRoute core.Responser `json:"-" yaml:"-"` // Optional, Default: core.StdResponse }
Route represents a runtime route.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router represents a http router handler to match and forward the http request to the upstream.
func (*Router) DelRoutesByIds ¶
DelRoutesByIds is the same as DelRoutes, but only uses the ids.
func (*Router) Handle ¶
Handle tries to match and handle the request.
If matching successfully, return true. Or, return false.
func (*Router) Serve ¶ added in v0.2.0
Serve is the same as ServeHTTP, but use Context as the input argument.
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP implements the interface http.Handler.
func (*Router) Use ¶ added in v0.2.0
func (r *Router) Use(mws ...middleware.Middleware)
Use appends the global middlewares that act on all the routes, which is not thread-safe and should be used only before running.