Documentation
¶
Overview ¶
Package light is a light weight http request multiplexer based on httprouter.
Index ¶
- Variables
- func Context(r *http.Request) context.Context
- func Params(r *http.Request) httprouter.Params
- func SetContext(ctx context.Context, r *http.Request)
- type Config
- type ConfigOption
- func WithHandleMethodNotAllowed(v bool) ConfigOption
- func WithMethodNotAllowed(f http.Handler) ConfigOption
- func WithNotFound(f http.Handler) ConfigOption
- func WithPanicHandler(f func(http.ResponseWriter, *http.Request, interface{})) ConfigOption
- func WithPrefix(prefix string) ConfigOption
- func WithRedirectFixedPath(v bool) ConfigOption
- func WithRedirectTrailingSlash(v bool) ConfigOption
- type ConfigOptionFunc
- type Handler
- func (h *Handler) Append(pattern string, subtree *Handler)
- func (h *Handler) Delete(pattern string, f http.HandlerFunc)
- func (h *Handler) Get(pattern string, f http.HandlerFunc)
- func (h *Handler) Handle(method, pattern string, f http.Handler)
- func (h *Handler) HandleAll(pattern string, f http.HandlerFunc)
- func (h *Handler) HandleFunc(method, pattern string, f http.HandlerFunc)
- func (h *Handler) Head(pattern string, f http.HandlerFunc)
- func (h *Handler) Options(pattern string, f http.HandlerFunc)
- func (h *Handler) Patch(pattern string, f http.HandlerFunc)
- func (h *Handler) Post(pattern string, f http.HandlerFunc)
- func (h *Handler) Put(pattern string, f http.HandlerFunc)
- func (h *Handler) ServeFiles(pattern string, root http.FileSystem)
- func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Use(mw ...chain.Constructor)
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = Config{ RedirectTrailingSlash: true, RedirectFixedPath: true, HandleMethodNotAllowed: true, }
DefaultConfig is the default Handler configuration used by New.
Functions ¶
func Context ¶
Context returns the context from the given request, or a new context.Background if it doesn't exist.
Types ¶
type Config ¶
type Config struct { // Prefix is the prefix for all paths in the handler. // Empty value is allowed and defaults to "/". Prefix string // Enables automatic redirection if the current route can't be matched but a // handler for the path with (without) the trailing slash exists. // For example if /foo/ is requested but a route only exists for /foo, the // client is redirected to /foo with http status code 301 for GET requests // and 307 for all other request methods. RedirectTrailingSlash bool // If enabled, the router tries to fix the current request path, if no // handle is registered for it. // First superfluous path elements like ../ or // are removed. // Afterwards the router does a case-insensitive lookup of the cleaned path. // If a handle can be found for this route, the router makes a redirection // to the corrected path with status code 301 for GET requests and 307 for // all other request methods. // For example /FOO and /..//Foo could be redirected to /foo. // RedirectTrailingSlash is independent of this option. RedirectFixedPath bool // If enabled, the router checks if another method is allowed for the // current route, if the current request can not be routed. // If this is the case, the request is answered with 'Method Not Allowed' // and HTTP status code 405. // If no other Method is allowed, the request is delegated to the NotFound // handler. HandleMethodNotAllowed bool // Configurable http.Handler which is called when no matching route is // found. If it is not set, http.NotFound is used. NotFound http.Handler // Configurable http.Handler which is called when a request // cannot be routed and HandleMethodNotAllowed is true. // If it is not set, http.Error with http.StatusMethodNotAllowed is used. MethodNotAllowed http.Handler // Function to handle panics recovered from http handlers. // It should be used to generate a error page and return the http error code // 500 (Internal Server Error). // The handler can be used to keep your server from crashing because of // unrecovered panics. // // No middleware is applied to the PanicHandler. PanicHandler func(http.ResponseWriter, *http.Request, interface{}) }
Config is the Handler configuration.
type ConfigOption ¶
type ConfigOption interface {
Set(c *Config)
}
ConfigOption is the interface for updating config options.
func WithHandleMethodNotAllowed ¶
func WithHandleMethodNotAllowed(v bool) ConfigOption
WithHandleMethodNotAllowed returns a ConfigOption that uptates the Config.
func WithMethodNotAllowed ¶
func WithMethodNotAllowed(f http.Handler) ConfigOption
WithMethodNotAllowed returns a ConfigOption that uptates the Config.
func WithNotFound ¶
func WithNotFound(f http.Handler) ConfigOption
WithNotFound returns a ConfigOption that uptates the Config.
func WithPanicHandler ¶
func WithPanicHandler(f func(http.ResponseWriter, *http.Request, interface{})) ConfigOption
WithPanicHandler returns a ConfigOption that uptates the Config.
func WithPrefix ¶
func WithPrefix(prefix string) ConfigOption
WithPrefix returns a ConfigOption that uptates the Config.
func WithRedirectFixedPath ¶
func WithRedirectFixedPath(v bool) ConfigOption
WithRedirectFixedPath returns a ConfigOption that uptates the Config.
func WithRedirectTrailingSlash ¶
func WithRedirectTrailingSlash(v bool) ConfigOption
WithRedirectTrailingSlash returns a ConfigOption that uptates the Config.
type ConfigOptionFunc ¶
type ConfigOptionFunc func(c *Config)
ConfigOptionFunc is an adapter for config option functions.
func (ConfigOptionFunc) Set ¶
func (f ConfigOptionFunc) Set(c *Config)
Set implements the ConfigOption interface.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is the http request multiplexer backed by httprouter.Router.
func New ¶
func New(opts ...ConfigOption) *Handler
New creates and initializes a new Handler using default settings and the given options.
func NewHandler ¶
NewHandler creates and initializes a new Handler with the given config.
func (*Handler) Append ¶
Append appends a handler to this handler, under the given pattern. All middleware from the root tree propagates to the subtree. However, the subtree's configuration such as prefix and fallback handlers, like NotFound and MethodNotAllowed, are ignored by the root tree in favor of its own configuration.
func (*Handler) Delete ¶
func (h *Handler) Delete(pattern string, f http.HandlerFunc)
Delete is a shortcut for mux.Handle("DELETE", path, handle)
func (*Handler) Get ¶
func (h *Handler) Get(pattern string, f http.HandlerFunc)
Get is a shortcut for mux.Handle("GET", path, handle)
func (*Handler) HandleAll ¶
func (h *Handler) HandleAll(pattern string, f http.HandlerFunc)
HandleAll will register a pattern with a handler for All requests.
func (*Handler) HandleFunc ¶
func (h *Handler) HandleFunc(method, pattern string, f http.HandlerFunc)
HandleFunc registers a new request handler with the given method and pattern.
func (*Handler) Head ¶
func (h *Handler) Head(pattern string, f http.HandlerFunc)
Head is a shortcut for mux.Handle("HEAD", path, handle)
func (*Handler) Options ¶
func (h *Handler) Options(pattern string, f http.HandlerFunc)
Options is a shortcut for mux.Handle("OPTIONS", path, handle)
func (*Handler) Patch ¶
func (h *Handler) Patch(pattern string, f http.HandlerFunc)
Patch is a shortcut for mux.Handle("PATCH", path, handle)
func (*Handler) Post ¶
func (h *Handler) Post(pattern string, f http.HandlerFunc)
Post is a shortcut for mux.Handle("POST", path, handle)
func (*Handler) Put ¶
func (h *Handler) Put(pattern string, f http.HandlerFunc)
Put is a shortcut for mux.Handle("PUT", path, handle)
func (*Handler) ServeFiles ¶
func (h *Handler) ServeFiles(pattern string, root http.FileSystem)
ServeFiles serves files from the given file system root.
The pattern must end with "/*filepath" to have files served from the local path /path/to/dir/*filepath.
For example, if root is "/etc" and *filepath is "passwd", the local file "/etc/passwd" is served. Because an http.FileServer is used internally it's possible that http.NotFound is called instead of httpmux's NotFound handler.
To use the operating system's file system implementation, use http.Dir: mux.ServeFiles("/src/*filepath", http.Dir("/var/www")).