Documentation
¶
Overview ¶
Package router provides a manager to add public and secure routes based on an http.Handler or http.HandlerFunc. Specific Action<->HTTP Method mapping can be defined. Middleware helpers to define middlewares with a strict order. Files or directories can be added. The PATTERN, PARAMS, ACTION and allowed HTTP Methods (only on OPTIONS) will be added as request context. The router is provider based. TODO Create a general middleware for all routes. TODO Files and Directories should also have the options for middlewares. maybe rename AddPublicFile in addFile with a secure boolean? same for AddRoute and AddDirectory.
Index ¶
Constants ¶
const ( // PARAMS of the provider are added as context to the HTTP context. PARAMS = registryPrefix + "params" // PATTERN of the route is added as context to the HTTP context. PATTERN = registryPrefix + "pattern" // ALLOWED HTTP methods of the route is added as HTTP context, if the request is http.MethodOptions. ALLOWED = registryPrefix + "allowedMethods" // ACTION is added to the HTTP context if the Route.Action was defined. ACTION = registryPrefix + "action" )
Request context keys.
const (
JSROUTER = "jsrouter"
)
pre-defined providers
Variables ¶
var ( ErrHandler = errors.New("route: handler must be of type http.Handler or http.HandlerFunc") ErrMapper = errors.New("route: a mapper with zero value is not allowed") ErrMethodUnique = "route: HTTP method %s is not unique on pattern %s" ErrActionMissing = "route: a action name is mandatory on http.Handler (pattern: %s)" )
Error messages.
var ( ErrHTTPMethod = "router: HTTP method %s is not allowed" ErrHTTPMethodPattern = ErrHTTPMethod + " on pattern %s" ErrPatternNotFound = "router: pattern %s is not defined" ErrPattern = errors.New("router: pattern must begin with a slash") ErrPatternExists = "router: pattern %s already exists" ErrSource = "router: source %s does not exist" ErrRootDir = errors.New("router: a directory can not be added on root level") ErrSecureMiddleware = errors.New("router: no secure middleware is defined") )
Error messages.
Functions ¶
func NewMiddleware ¶
func NewMiddleware(m ...mwHandlerFunc) *middleware
NewMiddleware creates an middleware chain. It can be empty or multiple mws can be added as argument.
Types ¶
type Manager ¶
type Manager interface { // Routes return all defined routes. Routes() []Route // RouteByPattern will return an error if the pattern does not exist. RouteByPattern(pattern string) (Route, error) // ActionByPatternMethod will return the action by the pattern and HTTP method. ActionByPatternMethod(pattern string, method string) string // AllowHTTPMethod allows to globally allow/disallow a HTTP Method. AllowHTTPMethod(method string, allow bool) error // SetSecureMiddleware for the secure routes. SetSecureMiddleware(*middleware) // SetFavicon for the server. The pattern will be "/favicon.ico". SetFavicon(source string) error // AddPublicFile to the router. If the source does not exist, an error will return. AddPublicFile(pattern string, source string) error // AddPublicDir to the router. Directories are not allowed on pattern root level "/". AddPublicDir(pattern string, source string) error // AddSecureRoute will add a route with all the secure middlewares to the router provider. AddSecureRoute(Route) error // AddPublicRoute to the router provider. AddPublicRoute(Route) error // Handler Handler() http.Handler // SetNotFound - a custom not found Handler can be added. SetNotFound(handler http.Handler) }
Manager interface of the router.
type Mapping ¶
type Mapping interface { // Action for the mapping. Action() string // Methods for the mapping. Methods() []string SetMethods([]string) // Middleware(s) of the mapping Middleware() *middleware }
Mapping interface.
func NewMapping ¶
NewMapping creates a new mapping with the required data. All by the router manager allowed HTTP methods can be uses. A HTTP Method must be unique on one pattern. Action can be a string or a function. Middlewares are copied.
type Provider ¶
type Provider interface { // HTTPHandler must return the mux for http/server. HTTPHandler() http.Handler // custom NotFound handler can be set. SetNotFound(http.Handler) // AddRoute to the router. AddRoute(Route) error // AddPublicDir to the router. // The source is already checked if it exists. AddPublicDir(url string, path string) error // AddPublicFile to the router // The source is already checked if it exists. AddPublicFile(url string, path string) error }
Provider interface.
type Route ¶
type Route interface { // Pattern of the route. Pattern() string // Handler of the route. // May be nil, Handler or HandlerFunc has a value. Handler() http.Handler // HandlerFunc of the route. // May be nil, Handler or HandlerFunc has a value. HandlerFunc() http.HandlerFunc // Mapping of the route. Mapping() []Mapping // Secure identifies if the rout was added with a secured middleware. Secure() bool // Error message. Error() error }
Route interface.
Directories
¶
Path | Synopsis |
---|---|
Package jsrouter implements the router.Provider interface and wraps the julienschmidt.httprouter.
|
Package jsrouter implements the router.Provider interface and wraps the julienschmidt.httprouter. |
Package middleware (logger) provides a simple middleware for the logger.Manager.
|
Package middleware (logger) provides a simple middleware for the logger.Manager. |
jwt
Package jwt provides a parser, generator and a middleware to checks if a jwt-token is valid.
|
Package jwt provides a parser, generator and a middleware to checks if a jwt-token is valid. |