Documentation ¶
Index ¶
- Variables
- func NewContext(ctx context.Context, m *MuxMatch) context.Context
- func StatusError(w http.ResponseWriter, r *http.Request, err error)
- type ErrorHandler
- type ErrorHandlerFunc
- type Handler
- type HandlerFunc
- type MethodSet
- type MuxMatch
- type ServeMux
- func (mux *ServeMux) Handle(method, pattern string, handler Handler)
- func (mux *ServeMux) HandleError(errHandler ErrorHandler)
- func (mux *ServeMux) HandleErrorFunc(errHandler ErrorHandlerFunc)
- func (mux *ServeMux) HandleFunc(method, pattern string, handler func(http.ResponseWriter, *http.Request) error)
- func (mux *ServeMux) HandleMethods(methods MethodSet, pattern string, handler Handler)
- func (mux *ServeMux) HandleMethodsFunc(methods MethodSet, pattern string, ...)
- func (mux *ServeMux) Lookup(r *http.Request) *MuxMatch
- func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (mux *ServeMux) ServeHTTPErr(w http.ResponseWriter, r *http.Request) error
Constants ¶
This section is empty.
Variables ¶
var ErrMuxNotFound = errors.New("mux match not found")
ErrMuxNotFound is returned by ServeMux when a matching handler was not found.
Functions ¶
func NewContext ¶
NewContext returns a new Context that carries value u.
func StatusError ¶
func StatusError(w http.ResponseWriter, r *http.Request, err error)
StatusError replies to a request with an appropriate status code and HTTP status text.
Types ¶
type ErrorHandler ¶
type ErrorHandler interface {
ErrorHTTP(w http.ResponseWriter, r *http.Request, err error)
}
ErrorHandler handles errors that arise while handling http requests.
func StatusErrorHandler ¶
func StatusErrorHandler() ErrorHandler
StatusErrorHandler returns a basic error handler that just returns a HTTP status error response. Any errors are logged before writing the response.
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
The ErrorHandlerFunc type is an adapter to allow functions to be used as HTTP error handlers.
func (ErrorHandlerFunc) ErrorHTTP ¶
func (f ErrorHandlerFunc) ErrorHTTP(w http.ResponseWriter, r *http.Request, err error)
ErrorHTTP calls f(w, err, code).
type Handler ¶
type Handler interface {
ServeHTTPErr(http.ResponseWriter, *http.Request) error
}
A Handler responds to an HTTP request. Handler is like http.Handler but may return an error.
func FallibleFunc ¶
FallibleFunc adapts an infallible http handler with no return value to return an error. The returned error is always nil.
type HandlerFunc ¶
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.
func (HandlerFunc) ServeHTTPErr ¶
func (f HandlerFunc) ServeHTTPErr(w http.ResponseWriter, r *http.Request) error
ServeHTTPErr calls f(w, r).
type MethodSet ¶
type MethodSet []string
MethodSet is a set of HTTP methods.
func AnyMethod ¶
func AnyMethod() MethodSet
AnyMethod returns a new MethodSet of all of the commonly known HTTP methods. The set of all methods is not known, thus this uses the more common interpretation of any method defined in RFC 7231 section 4.3 & RFC 5789.
func Methods ¶
Methods combines the given HTTP methods into a MethodSet. Duplicates are exluded to preserve set semantics.
type MuxMatch ¶
type MuxMatch struct {
// contains filtered or unexported fields
}
MuxMatch represents a matched handler for a given request. The MuxMatch provides access to the pattern that matched and the values extracted from the path for any dynamic parameters that appear in the pattern.
func FromContext ¶
FromContext returns the MuxMatch value stored in ctx, if any.
func (*MuxMatch) Handler ¶
Handler returns the handler registered for method. Handler returns nil if a handler is not registered for method.
func (*MuxMatch) Params ¶
Params returns the matched parameters from the URL in the order that they appear in the pattern.
type ServeMux ¶
type ServeMux struct {
// contains filtered or unexported fields
}
ServeMux is an HTTP request multiplexer. It matches the method and URL of each incoming request against a list of registered routes and calls the handler for the method and pattern that most closely matches the request.
Patterns name paths like "/users". A pattern may contain dynamic path segments. The syntax for patterns is a subset of the browser's URL Pattern API:
- Literal strings which will be matched exactly.
- Wildcards of the form "/users/*" match any string.
- Named groups of the form "/users/:id" match any string like wildcards, but assign a name that can be used to lookup the matched segment.
Placeholders may only appear between slashes, as in "/users/:id/profile", or as the last path segment, as in "/images/*".
Requests are matched by first looking for an exact match, then falling back to pattern matches. Thus the pattern "/users/new" would win over "/users/:id". The weight of named and un-named parameters is the same.
More specific matches are prioritized over less specific matches. For example, if both "/users" and "/users/:id" are registered, a request for "/users/1" would match "/users/:id".
If multiple routes are registered for the same method and pattern, even if the parameter names are different, ServeMux will panic.
func (*ServeMux) Handle ¶
Handle registers the handler for the given method and pattern. If a handler already exists for method and pattern, Handle panics.
func (*ServeMux) HandleError ¶
func (mux *ServeMux) HandleError(errHandler ErrorHandler)
HandleError registers the error handler for mux.
func (*ServeMux) HandleErrorFunc ¶
func (mux *ServeMux) HandleErrorFunc(errHandler ErrorHandlerFunc)
HandleErrorFunc registers the error handler function for mux.
func (*ServeMux) HandleFunc ¶
func (mux *ServeMux) HandleFunc(method, pattern string, handler func(http.ResponseWriter, *http.Request) error)
HandleFunc registers the handler function for the given method and pattern.
func (*ServeMux) HandleMethods ¶
Handle registers the handler for the given methods and pattern.
func (*ServeMux) HandleMethodsFunc ¶
func (mux *ServeMux) HandleMethodsFunc(methods MethodSet, pattern string, handler func(http.ResponseWriter, *http.Request) error)
HandleMethodsFunc registers the handler function for the given methods and pattern.
func (*ServeMux) ServeHTTP ¶
func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHttp implements http.Handler by dispatching the request to the handler whose method and pattern most closely matches the request URL.
func (*ServeMux) ServeHTTPErr ¶
ServeHTTPErr dispatches the request to the handler whose method and pattern most closely matches the request URL, forwarding any errors.