Documentation ¶
Overview ¶
Package mux is a high performance HTTP request router, also called multiplexer or just mux.
Example:
package main import ( "fmt" "log" "net/http" "github.com/pinub/mux" ) func index() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Welcome!\n") }) } func hello() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello\n") }) } func main() { m := mux.New() m.Get("/", index()) m.Get("/hello", hello()) log.Fatal(http.ListenAndServe(":8080", m)) }
The Muxer matches incoming requests by the method and path and delegates to that assiciated handler. Currently GET, POST, PUT, PATCH, DELETE and OPTIONS are supported methods.
Named parameters are not supported.
Path: /foo/bar
Requests:
/foo/bar matches the handler /foo/bar/ doesn't match, but redirects to /foo/bar /foo/foo doesn't match /foo doesn't match
Index ¶
- type Router
- func (r *Router) Delete(path string, h http.Handler)
- func (r *Router) Get(path string, h http.Handler)
- func (r *Router) Options(path string, h http.Handler)
- func (r *Router) Patch(path string, h http.Handler)
- func (r *Router) Post(path string, h http.Handler)
- func (r *Router) Put(path string, h http.Handler)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Router ¶
type Router struct { // Enables automatic redirection if the requested path doesn't match but // a handler with a path without the trailing slash exists. Default: true RedirectTrailingSlash bool // Enables 'Method Not Allowed' responses when a handler for the the // path, but not the requested method exists. Default: true HandleMethodNotAllowed bool // Custom http.Handler which is called when no handler was found for the // requested route. Defaults: http.NotFound. NotFound http.Handler // contains filtered or unexported fields }
Router is a http.Handler used to dispatch request to different handlers.
func New ¶
func New() *Router
New initializes the Router. All configurable options are enabled by default.
func (*Router) Delete ¶
Delete registers a new request handle for the DELETE method and the given path.
func (*Router) Options ¶
Options registers a new request handle for the OPTIONS method and the given path.
func (*Router) Patch ¶
Patch registers a new request handle for the PATCH method and the given path.