routing

package
v7.0.0-rc0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2022 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 1 more Imports: 82 Imported by: 0

Documentation

Overview

Package routing defines the HTTP routes for Traffic Ops and provides tools to register those routes with appropriate middleware.

Index

Constants

View Source
const Authenticated = true

Authenticated indicates that a route requires authentication for use.

View Source
const NoAuth = false

NoAuth indicates that a route does not require authentication for use.

View Source
const RoutePrefix = "^api" // TODO config?

RoutePrefix is a prefix that all API routes must match.

Variables

This section is empty.

Functions

func CompileRoutes

func CompileRoutes(routes map[string][]PathHandler) map[string][]CompiledRoute

CompileRoutes - takes a map of methods to paths and handlers, and returns a map of methods to CompiledRoutes

func CreateRouteMap

func CreateRouteMap(rs []Route, disabledRouteIDs []int, perlHandler http.HandlerFunc, authBase middleware.AuthBase, reqTimeOutSeconds int) (map[string][]PathHandler, map[api.Version]struct{})

CreateRouteMap returns a map of methods to a slice of paths and handlers; wrapping the handlers in the appropriate middleware. Uses Semantic Versioning: routes are added to every subsequent minor version, but not subsequent major versions. For example, a 1.2 route is added to 1.3 but not 2.1. Also truncates '2.0' to '2', creating succinct major versions. Returns the map of routes, and a map of API versions served.

func DBStatsHandler

func DBStatsHandler(db *sqlx.DB) http.HandlerFunc

func GetBackendConfig

func GetBackendConfig() config.BackendConfig

GetBackendConfig returns the current BackendConfig.

func GetRouteIDMap

func GetRouteIDMap(IDs []int) map[int]struct{}

GetRouteIDMap takes a []int Route IDs and converts it into a map for fast lookup.

func HandleBackendRoute

func HandleBackendRoute(cfg *config.Config, route config.BackendRoute, w http.ResponseWriter, r *http.Request) (error, error, int)

HandleBackendRoute does all the pre processing for the backend routes.

func Handler

func Handler(
	routes map[string][]CompiledRoute,
	versions map[api.Version]struct{},
	catchall http.Handler,
	db *sqlx.DB,
	cfg *config.Config,
	getReqID func() uint64,
	plugins plugin.Plugins,
	tv trafficvault.TrafficVault,
	w http.ResponseWriter,
	r *http.Request,
)

Handler - generic handler func used by the Handlers hooking into the routes

func IsRequestAPIAndUnknownVersion

func IsRequestAPIAndUnknownVersion(req *http.Request, versions map[api.Version]struct{}) bool

IsRequestAPIAndUnknownVersion returns true if the request starts with `/api` and is a version not in the list of versions.

func MemoryStatsHandler

func MemoryStatsHandler() http.HandlerFunc

func RegisterRoutes

func RegisterRoutes(d ServerData) error

RegisterRoutes - parses the routes and registers the handlers with the Go Router

func SetBackendConfig

func SetBackendConfig(backendConfig config.BackendConfig)

SetBackendConfig sets the BackendConfig to the value supplied.

Types

type CompiledRoute

type CompiledRoute struct {
	Handler http.HandlerFunc
	Regex   *regexp.Regexp
	Params  []string
	ID      int
}

CompiledRoute ...

type PathHandler

type PathHandler struct {
	Path    string
	Handler http.HandlerFunc
	ID      int
}

PathHandler ...

type Route

type Route struct {
	// Order matters! Do not reorder this! Routes() uses positional construction for readability.
	Version             api.Version
	Method              string
	Path                string
	Handler             http.HandlerFunc
	RequiredPrivLevel   int
	RequiredPermissions []string
	Authenticated       bool
	Middlewares         []middleware.Middleware
	ID                  int // unique ID for referencing this Route
}

A Route defines an association with a client request and a handler for that request.

func Routes

func Routes(d ServerData) ([]Route, http.Handler, error)

Routes returns the API routes, raw non-API root level routes, and a catchall route for when no route matches.

func (*Route) SetMiddleware

func (r *Route) SetMiddleware(authBase middleware.AuthBase, requestTimeout time.Duration)

SetMiddleware sets up a Route's Middlewares to include the default set of Middlewares if necessary.

func (Route) String

func (r Route) String() string

type ServerData

type ServerData struct {
	config.Config
	DB           *sqlx.DB
	Profiling    *bool // Yes this is a field in the config but we want to live reload this value and NOT the entire config
	Plugins      plugin.Plugins
	TrafficVault trafficvault.TrafficVault
	Mux          *http.ServeMux
}

ServerData ...

type ThrottledHandler

type ThrottledHandler struct {
	Handler http.Handler
	ReqChan chan struct{}
}

ThrottledHandler ...

func CreateThrottledHandler

func CreateThrottledHandler(handler http.Handler, maxConcurrentCalls int) ThrottledHandler

CreateThrottledHandler takes a handler, and a max and uses a channel to insure the handler is used concurrently by only max number of routines

func (ThrottledHandler) ServeHTTP

func (m ThrottledHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Directories

Path Synopsis
Package middleware provides symbols for HTTP "middleware" which wraps handlers to perform common behaviors, such as authentication, headers, and compression.
Package middleware provides symbols for HTTP "middleware" which wraps handlers to perform common behaviors, such as authentication, headers, and compression.

Jump to

Keyboard shortcuts

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