router

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Overview

Package router provides an entrypoint router for the api gateway.

Index

Constants

This section is empty.

Variables

View Source
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 })
)

AfterRoute is the next handler after the route.

View Source
var DefaultRouter = New()

DefaultRouter is the default global http router.

Functions

This section is empty.

Types

type Matcher

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

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

type MatcherFunc

type MatcherFunc func(r *http.Request) bool

MatcherFunc is a route matcher function.

func (MatcherFunc) Match

func (f MatcherFunc) Match(r *http.Request) bool

Match implements the interface Matcher.

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 New

func New() *Router

New returns a new router.

func (*Router) AddRoutes

func (r *Router) AddRoutes(routes ...Route)

AddRoutes adds the routes if they do not exist. Or. update them.

func (*Router) DelRoutes

func (r *Router) DelRoutes(routes ...Route)

DelRoutes deletes the routes if they exist. Or, do nothing.

func (*Router) DelRoutesByIds

func (r *Router) DelRoutesByIds(ids ...string)

DelRoutesByIds is the same as DelRoutes, but only uses the ids.

func (*Router) GetRoute

func (r *Router) GetRoute(id string) (Route, bool)

GetRoute returns the route by the route id.

func (*Router) Handle

func (r *Router) Handle(c *core.Context) (handled bool)

Handle tries to match and handle the request.

If matching successfully, return true. Or, return false.

func (*Router) Routes

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

Routes returns the added the routes, which are read-only.

func (*Router) Serve added in v0.2.0

func (r *Router) Serve(c *core.Context)

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.

Jump to

Keyboard shortcuts

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