Documentation ¶
Overview ¶
Package mixmux wraps HTTPRouter and HTTPTreeMux to provide consistent and idiomatic APIs, along with route grouping. Multiplexer-based parameter handling is bypassed.
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "net/http/httptest" "github.com/codemodus/mixmux" ) const ( Get = "GET" Post = "POST" Head = "HEAD" ) func main() { handler := http.HandlerFunc(methodHandler) mux := mixmux.NewRouter(nil) mux.Get("/get", handler) mux.Post("/post", handler) muxGroup := mux.Group("/grouped") muxGroup.Get("/get0", handler) muxGroup.Get("/get1", handler) server := httptest.NewServer(mux) rBody0, err := getReqBody(server.URL+"/get", Get) if err != nil { fmt.Println(err) } rBody1, err := getReqBody(server.URL+"/post", Post) if err != nil { fmt.Println(err) } rBodyGrouped0, err := getReqBody(server.URL+"/grouped/get0", Get) if err != nil { fmt.Println(err) } rBodyGrouped1, err := getReqBody(server.URL+"/grouped/get1", Get) if err != nil { fmt.Println(err) } fmt.Println("Get Body:", rBody0) fmt.Println("Post Body:", rBody1) fmt.Println("Grouped Bodies:", rBodyGrouped0, rBodyGrouped1) } func getReqBody(url, method string) (string, error) { r, err := http.NewRequest(method, url, nil) cl := &http.Client{} resp, err := cl.Do(r) if err != nil { return "", err } body, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } if len(body) == 0 { body = []byte(resp.Header.Get(Head)) } _ = resp.Body.Close() return string(body), nil } func methodHandler(w http.ResponseWriter, r *http.Request) { if r.Method == Head { w.Header().Add(Head, Head) } _, _ = w.Write([]byte(r.Method)) return }
Output: Get Body: GET Post Body: POST Grouped Bodies: GET GET
Index ¶
- type Mux
- type Options
- type Router
- func (m *Router) Any(path string, h http.Handler)
- func (m *Router) CORSMethods(path string, handlerWrappers ...func(http.Handler) http.Handler)
- func (m *Router) Connect(path string, h http.Handler)
- func (m *Router) Delete(path string, h http.Handler)
- func (m *Router) Get(path string, h http.Handler)
- func (m *Router) Group(path string) *Router
- func (m *Router) GroupMux(path string) Mux
- func (m *Router) Head(path string, h http.Handler)
- func (m *Router) Options(path string, h http.Handler)
- func (m *Router) Patch(path string, h http.Handler)
- func (m *Router) Post(path string, h http.Handler)
- func (m *Router) Put(path string, h http.Handler)
- func (m *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (m *Router) Trace(path string, h http.Handler)
- type TreeMux
- func (m *TreeMux) Any(path string, h http.Handler)
- func (m *TreeMux) CORSMethods(path string, handlerWrappers ...func(http.Handler) http.Handler)
- func (m *TreeMux) Connect(path string, h http.Handler)
- func (m *TreeMux) Delete(path string, h http.Handler)
- func (m *TreeMux) Get(path string, h http.Handler)
- func (m *TreeMux) Group(path string) *TreeMux
- func (m *TreeMux) GroupMux(path string) Mux
- func (m *TreeMux) Head(path string, h http.Handler)
- func (m *TreeMux) Options(path string, h http.Handler)
- func (m *TreeMux) Patch(path string, h http.Handler)
- func (m *TreeMux) Post(path string, h http.Handler)
- func (m *TreeMux) Put(path string, h http.Handler)
- func (m *TreeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (m *TreeMux) Trace(path string, h http.Handler)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Mux ¶
type Mux interface { http.Handler GroupMux(path string) Mux Any(path string, h http.Handler) Options(path string, h http.Handler) Get(path string, h http.Handler) Post(path string, h http.Handler) Put(path string, h http.Handler) Patch(path string, h http.Handler) Delete(path string, h http.Handler) Head(path string, h http.Handler) Trace(path string, h http.Handler) Connect(path string, h http.Handler) CORSMethods(path string, handlerWrappers ...func(http.Handler) http.Handler) }
Mux ...
type Options ¶
type Options struct { RedirectTrailingSlash bool RedirectFixedPath bool HandleMethodNotAllowed bool NotFound http.Handler MethodNotAllowed http.Handler }
Options holds available options for a new Router.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router wraps HTTPRouter.
func (*Router) CORSMethods ¶
CORSMethods ... TODO:
func (*Router) GroupMux ¶
GroupMux takes a path and returns a new Router wrapping the original Router.
type TreeMux ¶
type TreeMux struct {
// contains filtered or unexported fields
}
TreeMux wraps HTTPTreeMux.
func (*TreeMux) CORSMethods ¶
CORSMethods ... TODO:
func (*TreeMux) GroupMux ¶
GroupMux takes a path and returns a new TreeMux wrapping the original TreeMux.
Click to show internal directories.
Click to hide internal directories.