Documentation ¶
Overview ¶
Package mux implements a HTTP request multiplexer.
Index ¶
Examples ¶
Constants ¶
const ( MethodAll = mx.MethodAll MethodGet = mx.MethodGet MethodHead = mx.MethodHead MethodPost = mx.MethodPost MethodPut = mx.MethodPut MethodPatch = mx.MethodPatch MethodDelete = mx.MethodDelete MethodConnect = mx.MethodConnect MethodOptions = mx.MethodOptions MethodTrace = mx.MethodTrace )
Common HTTP methods.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Muxer ¶ added in v0.0.77
type Muxer struct {
// contains filtered or unexported fields
}
Muxer is a HTTP request multiplexer.
It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL. It implements http.Handler
Use New to get a valid Muxer.
Example ¶
package main import ( "context" "fmt" "net/http" "os" "github.com/komuw/ong/config" "github.com/komuw/ong/log" "github.com/komuw/ong/mux" ) func LoginHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, "welcome to your favorite website.") } } func BooksByAuthorHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { author := mux.Param(r.Context(), "author") _, _ = fmt.Fprintf(w, "fetching books by author: %s", author) } } func main() { l := log.New(context.Background(), os.Stdout, 1000) mx := mux.New( config.WithOpts("localhost", 8080, "super-h@rd-Pas1word", config.DirectIpStrategy, l), nil, mux.NewRoute( "login/", mux.MethodGet, LoginHandler(), ), mux.NewRoute( "/books/:author", mux.MethodAll, BooksByAuthorHandler(), ), ) server := &http.Server{ Handler: mx, Addr: ":8080", } err := server.ListenAndServe() if err != nil { panic(err) } }
Output:
func New ¶
New returns a HTTP request multiplexer that has the paths in routes.
notFoundHandler is the handler that will be used if a url is not found. If it is nil, http.NotFound is used instead.
All the paths of an application should be added as part of the routes slice argument. Typically, an application should only have one mux.
It panics with a helpful error message if it detects conflicting routes.
func (Muxer) GoString ¶ added in v0.0.77
GoString implements fmt.GoStringer
func (Muxer) Resolve ¶ added in v0.0.77
Resolve resolves a URL path to its corresponding Route and hence http handler. If no corresponding route/handler is found, a zero value Route is returned.
It is not intended for use in production settings, it is more of a dev/debugging tool. It is inspired by django's resolve url utility.
Example ¶
package main import ( "context" "fmt" "net/http" "os" "github.com/komuw/ong/config" "github.com/komuw/ong/log" "github.com/komuw/ong/mux" ) func LoginHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, "welcome to your favorite website.") } } func BooksByAuthorHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { author := mux.Param(r.Context(), "author") _, _ = fmt.Fprintf(w, "fetching books by author: %s", author) } } func main() { l := log.New(context.Background(), os.Stdout, 1000) mx := mux.New( config.WithOpts("localhost", 8080, "super-h@rd-Pas1word", config.DirectIpStrategy, l), nil, mux.NewRoute( "login/", mux.MethodGet, LoginHandler(), ), mux.NewRoute( "/books/:author", mux.MethodAll, BooksByAuthorHandler(), ), ) fmt.Println(mx.Resolve("nonExistentPath")) fmt.Println(mx.Resolve("login/")) fmt.Println(mx.Resolve("https://localhost/books/SidneySheldon")) }
Output:
func (Muxer) ServeHTTP ¶ added in v0.0.77
func (m Muxer) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements a http.Handler
It routes incoming http requests based on method and path extracting path parameters as it goes.
func (Muxer) String ¶ added in v0.0.77
String implements fmt.Stringer