Documentation ¶
Overview ¶
Package mux wraps gorilla/mux router with custom HandlerFunc and MiddlewareFunc.
Index ¶
- func Vars(r *http.Request) map[string]string
- type ErrorHandlerFunc
- type HTTPError
- func (e *HTTPError) Error() string
- func (e *HTTPError) MarshalJSON() ([]byte, error)
- func (e *HTTPError) UnmarshalJSON(b []byte) error
- func (e *HTTPError) WithErrorID(id string) *HTTPError
- func (e *HTTPError) WithInternalError(err error) *HTTPError
- func (e *HTTPError) WithInternalMessage(message string) *HTTPError
- func (e *HTTPError) WithShowError(flag bool) *HTTPError
- type HandlerFunc
- type MiddlewareFunc
- type Route
- func (r *Route) BuildOnly() *Route
- func (r *Route) BuildVarsFunc(f gorillamux.BuildVarsFunc) *Route
- func (r *Route) GetError() error
- func (r *Route) GetHandler() http.Handler
- func (r *Route) GetHostTemplate() (string, error)
- func (r *Route) GetMethods() ([]string, error)
- func (r *Route) GetName() string
- func (r *Route) GetPathRegexp() (string, error)
- func (r *Route) GetPathTemplate() (string, error)
- func (r *Route) GetQueriesRegexp() ([]string, error)
- func (r *Route) GetQueriesTemplates() ([]string, error)
- func (r *Route) Handler(h http.Handler) *Route
- func (r *Route) HandlerFunc(fn HandlerFunc) *Route
- func (r *Route) HandlerFuncBypass(fn func(http.ResponseWriter, *http.Request)) *Route
- func (r *Route) Headers(pairs ...string) *Route
- func (r *Route) HeadersRegexp(pairs ...string) *Route
- func (r *Route) Host(tpl string) *Route
- func (r *Route) Match(req *http.Request, match *gorillamux.RouteMatch) bool
- func (r *Route) MatcherFunc(f gorillamux.MatcherFunc) *Route
- func (r *Route) Methods(methods ...string) *Route
- func (r *Route) Name(name string) *Route
- func (r *Route) Path(tpl string) *Route
- func (r *Route) PathPrefix(tpl string) *Route
- func (r *Route) Queries(pairs ...string) *Route
- func (r *Route) Schemes(schemes ...string) *Route
- func (r *Route) SkipClean() bool
- func (r *Route) Subrouter() *Router
- func (r *Route) URL(pairs ...string) (*url.URL, error)
- func (r *Route) URLHost(pairs ...string) (*url.URL, error)
- func (r *Route) URLPath(pairs ...string) (*url.URL, error)
- type Router
- func (r *Router) BuildVarsFunc(f gorillamux.BuildVarsFunc) *Route
- func (r *Router) Get(name string) *Route
- func (r *Router) Handle(path string, h http.Handler) *Route
- func (r *Router) HandleFunc(path string, fn HandlerFunc) *Route
- func (r *Router) HandleFuncBypass(path string, fn http.HandlerFunc) *Route
- func (r *Router) Headers(pairs ...string) *Route
- func (r *Router) Host(tpl string) *Route
- func (r *Router) MatcherFunc(f gorillamux.MatcherFunc) *Route
- func (r *Router) Methods(methods ...string) *Route
- func (r *Router) Name(name string) *Route
- func (r *Router) NewRoute() *Route
- func (r *Router) Path(tpl string) *Route
- func (r *Router) PathPrefix(tpl string) *Route
- func (r *Router) Queries(pairs ...string) *Route
- func (r *Router) Schemes(schemes ...string) *Route
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) SkipClean(value bool) *Router
- func (r *Router) StrictSlash(value bool) *Router
- func (r *Router) Use(mwf ...MiddlewareFunc)
- func (r *Router) UseBypass(mwf ...gorillamux.MiddlewareFunc)
- func (r *Router) UseEncodedPath() *Router
- func (r *Router) Walk(walkFn gorillamux.WalkFunc) error
- type Wrapper
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(err error, w http.ResponseWriter, r *http.Request)
ErrorHandlerFunc handles error returned by `Handler`.
type HTTPError ¶ added in v1.2.0
type HTTPError struct { Code int Message string InternalError error InternalMessage string ErrorID string ShowError bool }
HTTPError holds http error info.
Example ¶
package main import ( "encoding/json" "errors" "fmt" "net/http" "github.com/danikarik/mux" ) func main() { err := mux.NewHTTPError(http.StatusBadRequest, "Bad Request") data, _ := json.Marshal(err) fmt.Println(string(data)) opts := []func(e *mux.HTTPError){ func(e *mux.HTTPError) { e.ShowError = true }, func(e *mux.HTTPError) { e.InternalError = errors.New("Unexpected Error") }, func(e *mux.HTTPError) { e.InternalMessage = "Failed" }, func(e *mux.HTTPError) { e.ErrorID = "123" }, } err = mux.NewHTTPError(http.StatusInternalServerError, "Server Error", opts...) data, _ = json.Marshal(err) fmt.Println(string(data)) err = mux.NewHTTPError(http.StatusUnauthorized, "Unauthorized"). WithInternalMessage("Failed"). WithErrorID("456"). WithShowError(true) data, _ = json.Marshal(err) fmt.Println(string(data)) }
Output: {"code":400,"message":"Bad Request"} {"code":500,"message":"Server Error","internalError":"Unexpected Error","internalMessage":"Failed","id":"123"} {"code":401,"message":"Unauthorized","internalMessage":"Failed","id":"456"}
func NewHTTPError ¶ added in v1.2.0
NewHTTPError returns a new instance of `HTTPError`.
func (*HTTPError) MarshalJSON ¶ added in v1.2.0
MarshalJSON implemenets `json.Marshal`.
func (*HTTPError) UnmarshalJSON ¶ added in v1.2.0
UnmarshalJSON implements `json.Unmarshal`.
func (*HTTPError) WithErrorID ¶ added in v1.2.0
WithErrorID updates `ErrorID` field.
func (*HTTPError) WithInternalError ¶ added in v1.2.0
WithInternalError updates `InternalError` field.
func (*HTTPError) WithInternalMessage ¶ added in v1.2.0
WithInternalMessage updates `InternalMessage` field.
func (*HTTPError) WithShowError ¶ added in v1.2.0
WithShowError updates `ShowError` field.
type HandlerFunc ¶
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
HandlerFunc wraps standard `http.HandlerFunc` with `error` return value.
type MiddlewareFunc ¶
MiddlewareFunc wraps standard `http.Handler` middleware style with context and error.
type Route ¶ added in v1.1.0
type Route struct { Wrapper Wrapper // contains filtered or unexported fields }
Route stores information to match a request and build URLs.
func CurrentRoute ¶
CurrentRoute returns the matched route for the current request, if any.
func NewRoute ¶ added in v1.1.0
func NewRoute(r *gorillamux.Route, opts ...func(*Route)) *Route
NewRoute returns a new route instance.
func (*Route) BuildOnly ¶ added in v1.1.0
BuildOnly sets the route to never match: it is only used to build URLs.
func (*Route) BuildVarsFunc ¶ added in v1.1.0
func (r *Route) BuildVarsFunc(f gorillamux.BuildVarsFunc) *Route
BuildVarsFunc adds a custom function to be used to modify build variables before a route's URL is built.
func (*Route) GetError ¶ added in v1.1.0
GetError returns an error resulted from building the route, if any.
func (*Route) GetHandler ¶ added in v1.1.0
GetHandler returns the handler for the route, if any.
func (*Route) GetHostTemplate ¶ added in v1.1.0
GetHostTemplate returns the template used to build the route match.
func (*Route) GetMethods ¶ added in v1.1.0
GetMethods returns the methods the route matches against.
func (*Route) GetPathRegexp ¶ added in v1.1.0
GetPathRegexp returns the expanded regular expression used to match route path.
func (*Route) GetPathTemplate ¶ added in v1.1.0
GetPathTemplate returns the template used to build the route match.
func (*Route) GetQueriesRegexp ¶ added in v1.1.0
GetQueriesRegexp returns the expanded regular expressions used to match the route queries.
func (*Route) GetQueriesTemplates ¶ added in v1.1.0
GetQueriesTemplates returns the templates used to build the query matching.
func (*Route) HandlerFunc ¶ added in v1.1.0
func (r *Route) HandlerFunc(fn HandlerFunc) *Route
HandlerFunc sets a handler function for the route.
func (*Route) HandlerFuncBypass ¶ added in v1.1.0
HandlerFuncBypass sets a handler function for the route.
func (*Route) HeadersRegexp ¶ added in v1.1.0
HeadersRegexp accepts a sequence of key/value pairs, where the value has regex support.
func (*Route) Match ¶ added in v1.1.0
func (r *Route) Match(req *http.Request, match *gorillamux.RouteMatch) bool
Match matches the route against the request.
func (*Route) MatcherFunc ¶ added in v1.1.0
func (r *Route) MatcherFunc(f gorillamux.MatcherFunc) *Route
MatcherFunc adds a custom function to be used as request matcher.
func (*Route) Name ¶ added in v1.1.0
Name sets the name for the route, used to build URLs. It is an error to call Name more than once on a route.
func (*Route) PathPrefix ¶ added in v1.1.0
PathPrefix adds a matcher for the URL path prefix. This matches if the given template is a prefix of the full URL path. See Route.Path() for details on the tpl argument.
func (*Route) SkipClean ¶ added in v1.1.0
SkipClean reports whether path cleaning is enabled for this route via Router.SkipClean.
type Router ¶
type Router struct { Wrapper Wrapper NotFoundHandler HandlerFunc MethodNotAllowedHandler HandlerFunc // contains filtered or unexported fields }
Router wraps `github.com/gorilla/mux` with custom `mux.Handler`.
func NewRouterWithMux ¶ added in v1.1.0
func NewRouterWithMux(mux *gorillamux.Router, opts ...func(*Router)) *Router
NewRouterWithMux returns a new router instance with input mux.
func (*Router) BuildVarsFunc ¶
func (r *Router) BuildVarsFunc(f gorillamux.BuildVarsFunc) *Route
BuildVarsFunc registers a new route with a custom function for modifying route variables before building a URL.
func (*Router) Handle ¶
Handle registers a new route with a matcher for the URL path. See Route.Path() and Route.Handler().
func (*Router) HandleFunc ¶
func (r *Router) HandleFunc(path string, fn HandlerFunc) *Route
HandleFunc registers a new route with a matcher for the URL path. See Route.Path() and Route.HandlerFunc().
func (*Router) HandleFuncBypass ¶
func (r *Router) HandleFuncBypass(path string, fn http.HandlerFunc) *Route
HandleFuncBypass registers a new route with a matcher for the URL path. See Route.Path() and Route.HandlerFunc().
func (*Router) Headers ¶
Headers registers a new route with a matcher for request header values. See Route.Headers().
func (*Router) MatcherFunc ¶
func (r *Router) MatcherFunc(f gorillamux.MatcherFunc) *Route
MatcherFunc registers a new route with a custom matcher function. See Route.MatcherFunc().
func (*Router) Methods ¶
Methods registers a new route with a matcher for HTTP methods. See Route.Methods().
func (*Router) PathPrefix ¶
PathPrefix registers a new route with a matcher for the URL path prefix. See Route.PathPrefix().
func (*Router) Queries ¶
Queries registers a new route with a matcher for URL query values. See Route.Queries().
func (*Router) Schemes ¶
Schemes registers a new route with a matcher for URL schemes. See Route.Schemes().
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP dispatches the handler registered in the matched route.
func (*Router) SkipClean ¶
SkipClean defines the path cleaning behaviour for new routes. The initial value is false.
func (*Router) StrictSlash ¶
StrictSlash defines the trailing slash behavior for new routes. The initial value is false.
func (*Router) Use ¶
func (r *Router) Use(mwf ...MiddlewareFunc)
Use appends a MiddlewareFunc to the chain.
func (*Router) UseBypass ¶
func (r *Router) UseBypass(mwf ...gorillamux.MiddlewareFunc)
UseBypass appends a gorilla's `mux.MiddlewareFunc` to the chain.
func (*Router) UseEncodedPath ¶
UseEncodedPath tells the router to match the encoded original path to the routes.
type Wrapper ¶ added in v1.1.0
type Wrapper interface { ServeHandler(HandlerFunc) func(http.ResponseWriter, *http.Request) HandlerFunc(HandlerFunc) http.HandlerFunc ServeMiddleware(MiddlewareFunc) func(http.Handler) http.Handler MiddlewareFunc(MiddlewareFunc) func(http.Handler) http.Handler HandleError(error, http.ResponseWriter, *http.Request) }
Wrapper defines route wrapping methods and error handling for `Router` and `Route`.
func NewDefaultWrapper ¶ added in v1.1.0
func NewDefaultWrapper(fn ErrorHandlerFunc) Wrapper
NewDefaultWrapper returns a new default wrapper.