routing

package
v5.1.2+incompatible Latest Latest
Warning

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

Go to latest
Published: May 6, 2021 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 1 more Imports: 80 Imported by: 5

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 ...

View Source
const NoAuth = false

NoAuth ...

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

RoutePrefix ...

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, rawRoutes []RawRoute, perlRouteIDs, 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 GetRouteIDMap

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

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

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,
	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 Routes

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

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

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 RawRoute

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

RawRoute is an HTTP route to be served at the root, rather than under /api/version. Raw Routes should be rare, and almost exclusively converted old Perl routes which have yet to be moved to an API path.

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
	Authenticated     bool
	Middlewares       []middleware.Middleware
	ID                int  // unique ID for referencing this Route
	CanBypassToPerl   bool // if true, this Route can be passed through to Perl
}

Route ...

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
}

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