Documentation ¶
Overview ¶
Package router provides a fast and simple, security orientated HTTP router for GO (golang)
Index ¶
Constants ¶
const ( // DefaultHost contains the default host the router uses to match a route to a HTTP request. DefaultHost = "DEFAULT" // DefaultFallback defines the default behavior of the router whether to fallback to a subpath of a request path. // Example: the request contains a request for /foo/bar, but /foo/bar is not registered as a route. // DefaultFallback determines whether the routers is allowed to fallback to /foo if it is registered. // pathFallback can be set for each individual method route. DefaultFallback = false )
Variables ¶
This section is empty.
Functions ¶
func StripHostPort ¶
StripHostPort returns h without any trailing ":<port>".
Types ¶
type ErrorHandler ¶
type ErrorHandler func(w http.ResponseWriter, r *http.Request, code int)
ErrorHandler is a type of func(w http.ResponseWriter, r *http.Request, code int) where code corresponds to a HTTP status code (200, 404, 405 etc.). By default SRouter uses the http.Error to return errors. Th ErrorHandler type allows users to define their own implementation of an ErrorHandler to handle HTTP errors from the Router.
type Middleware ¶
Middleware is an inteface used to execute middleware functions by the router
type MiddlewareHandler ¶
MiddlewareHandler is a function that implements the Middleware interface using http.Handler
func (MiddlewareHandler) MiddlewareHTTP ¶
func (mh MiddlewareHandler) MiddlewareHTTP(handler http.Handler) http.Handler
MiddlewareHTTP implements the Middleware interface for MiddlewareHandler
type MiddlewareHandlerFunc ¶
type MiddlewareHandlerFunc func(handler http.HandlerFunc) http.HandlerFunc
MiddlewareHandlerFunc is a function that implements the Middleware interface using http.HandlerFunc
func (MiddlewareHandlerFunc) MiddlewareHTTP ¶
func (mhf MiddlewareHandlerFunc) MiddlewareHTTP(handler http.Handler) http.Handler
MiddlewareHTTP implements the Middleware interface for MiddlewareHandler
type SRouter ¶
type SRouter struct { // ErrorHandler allows to define a custom handler for errors. It takes ErrorHandler as type, // which implements the http.Error function (w http.ResponseWriter, error string, code int). ErrorHandler ErrorHandler // WebDAV. If enabled, the router allows WebDAV methods to be registered as routes WebDAV bool // contains filtered or unexported fields }
SRouter dispatches HTTP requests to a defined handler. This router implements the http.Handler and http.HandlerFunc to dispatch requests. Requests will be dispatched to defined routes. This router dispatches requests that match the request's: host, path, method, and Content-Type.
Requests that don't match the host or path will receive a standard HTTP 404 error. Requests that don't match the method for the host or path will receive a standard HTTP 405 error. Requests that don't match the Content-Type for the host or path will receive a standard HTTP 406 error.
By design router.SRouter requires explicit defition of routes. It does however support fallback to subpaths, if the request path cannot be found. Example: the request contains a request for /foo/bar, but /foo/bar is not registered as a route. router.SRouter will dispatch that request to /foo route if that route is registered and supports fallback.
func (*SRouter) AddRoute ¶
func (sr *SRouter) AddRoute(host, path string, fallback bool, method, content string, handler http.Handler)
AddRoute can be used to add a route to the Router Parameters:
1. Host as string. You should use router.DefaultHost if you do not want to use a custom host.
2. path as string. Every path should start with a /
3. path fallback as bool.
4. method as string. This can only be one single HTTP method
5. Content-Type. This allows multiple entries, separated by semicolon. For example "text/html;application/json" An empty Content-Type can also be valid, use a single semicolon to do so. For example ";" Character sets are not checked by router.SRouter so you do not have to define these explicitly
6. handler of type http.Handler or http.HandlerFunc. The http.HandlerFunc does implement the http.Handler interface and can therefore be passed into AddRoute as well.
func (*SRouter) UseMiddleware ¶
func (sr *SRouter) UseMiddleware(host, path string, handler Middleware)
UseMiddleware can be used to add Middleware to path routes