Documentation ¶
Overview ¶
Package dispatcher http middleware routing and dispatcher. Based on https://gitlab.com/krobolt/go-router
Dispatcher adds named routes, http handlers and middlware to go-router.
Handler:
func (w http.ResponseWriter, r *http.Request)
Middleware can be attached to named routes, (FIFO) which can executed before and after the handler using the following pattern:
example := func() Middleware { return func(f http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { //Code to be executed before handler //Execute handler f(w, r) //Code be to executed after handler } } }
@See gitlab.com/krobolt/go-router for defining routes/route patterns
Example Attach new route with "global" namespacec and "homepage" attached to index
dir := "/" options := router.NewDefaultOptions() names := []string{"global"} dispatcher := dispatcher.NewDispatcher(dir, options, names) dispatcher.Add("GET", "/", home, []string{"homepage"})
Attach middleware to "homepage"
dispatcher.Set("homepage", example)
Index ¶
- func BuildQueryFromVars(vars map[string]interface{}) string
- func ETag(key string, days int, w http.ResponseWriter, r *http.Request) bool
- func GenerateMiddlewareChain(f http.HandlerFunc, middlewares ...Middleware) http.HandlerFunc
- func GetHandlerFromHandleFunc(h http.HandlerFunc) http.Handler
- func Serve(port string, h http.Handler, logger *log.Logger)
- func ServeTLS(port string, h http.Handler, logger *log.Logger, cert string, key string)
- type Dispatcher
- type Handler
- type Middleware
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildQueryFromVars ¶
BuildQueryFromVars
func GenerateMiddlewareChain ¶
func GenerateMiddlewareChain(f http.HandlerFunc, middlewares ...Middleware) http.HandlerFunc
GenerateMiddlewareChain wraps http.HandlerFunc with middleware collection and returns http.HandlerFunc
func GetHandlerFromHandleFunc ¶
func GetHandlerFromHandleFunc(h http.HandlerFunc) http.Handler
GetHandlerFromHandleFunc wraps a http.HandlerFunc (SimpleHandle) and returns a http.Handler for example SimpleHandle can be passed to GetHandlerFromHandleFunc to be used in GenerateMiddlewareChain to create a http.HandlerFunc collection of SimpleHandle and Middlewares
Types ¶
type Dispatcher ¶
type Dispatcher interface { ServeHTTP(w http.ResponseWriter, r *http.Request) Group(path string, callback func(), name []string) Add(method string, path string, handle http.HandlerFunc, names []string) error Match(method string, path string) Response GetMiddleware(name string) (func() Middleware, error) GetRouteMiddlewares(names []string) ([]Middleware, error) SetMiddleware(name string, m func() Middleware) error AddHandle(code int, h http.HandlerFunc) }
Dispatcher is responsible for linking a route id to an http.Handlerfunc
func NewDispatcher ¶
func NewDispatcher(dir string, options router.Options, names []string) Dispatcher
NewDispatcher returns new Dispatcher
type Handler ¶
type Handler interface { GetHandler() http.HandlerFunc GetNames() []string GetID() string }
type Middleware ¶
type Middleware func(http.HandlerFunc) http.HandlerFunc
Middleware is a wrapped http.HandlerFunc