Categorygithub.com/go-web/httpmux
modulepackage
0.0.0-20160505070239-9e95425ee2c3
Repository: https://github.com/go-web/httpmux.git
Documentation: pkg.go.dev

# README

httpmux

GoDoc GoReportCard

httpmux is an http request multiplexer for Go built on top of the popular httprouter, with modern features.

The main motivation is to bring http handlers back to their original Handler interface as defined by net/http but leverage the speed and features of httprouter, such as dispatching handlers by method and handling HTTP 405 automatically.

Another important aspect of httpmux is that it provides request context for arbitrary data, such as httprouter's URL parameters. Seasoned gophers migth immediately think this is an overlap of gorilla's context package, however, their implementation rely on global variables and mutexes that can cause contention on heavily loaded systems. We use a different approach, which was stolen from httpway, that hijacks the http.Request's Body field and replace it with an object that is an io.ReadCloser but also carries a net/context object. This works well for middleware that wants to store arbitrary data in the request, serial, and once it hits your handler, the context can be passed around to goroutines. It is automatically cleared at the end of the middleware chain.

There's been discussions for adding net/context to the standard library but most options require changing or creating a new interface and/or function signature for http handlers. In httpmux we remain close to net/http aiming at being more pluggable and composable with existing code in the wild, and if the Request type ends up getting a Context field, will be an easy change in httpmux.

To make contexts more useful, httpmux provides the ability to register and chain wrapper handlers, middleware. Our implementation is based on blogs and especially chi, but much smaller.

Last but not least, httpmux offers two more features for improving composability. First, is to configure a global prefix for all handlers in the multiplexer. This is for cases when you have to run your API behind a proxy, or mixed with other services, and have to be able to parse and understand the prefix in your handlers. Second, is to allow subtrees like gin's groups, but in a more composable way. Think of cases where your code is an independent package that provides an http handler, that is tested and run isolated, but can be added to a larger API at run time. In chi, this is equivalent to the Mount function in their router.

# Functions

Context returns the context from the given request, or a new context.Background if it doesn't exist.
New creates and initializes a new Handler using default settings and the given options.
NewHandler creates and initializes a new Handler with the given config.
Params returns the httprouter.Params from the request context.
SetContext updates the given context in the request if the request has been previously instrumented by httpmux.
WithHandleMethodNotAllowed returns a ConfigOption that uptates the Config.
WithMethodNotAllowed returns a ConfigOption that uptates the Config.
WithMiddleware returns a ConfigOption that uptates the Config.
WithMiddlewareFunc returns a ConfigOption that uptates the Config.
WithNotFound returns a ConfigOption that uptates the Config.
WithPanicHandler returns a ConfigOption that uptates the Config.
WithPrefix returns a ConfigOption that uptates the Config.
WithRedirectFixedPath returns a ConfigOption that uptates the Config.
WithRedirectTrailingSlash returns a ConfigOption that uptates the Config.

# Variables

DefaultConfig is the default Handler configuration used by New.

# Structs

No description provided by the author
No description provided by the author

# Interfaces

ConfigOption is the interface for updating config options.

# Type aliases

ConfigOptionFunc is an adapter for config option functions.
No description provided by the author
No description provided by the author