Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/go-michi/michi" ) func main() { h := func(name string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Println(name + " handler") }) } mid := func(name string) func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Println(name + " start") next.ServeHTTP(w, r) fmt.Println(name + " end") }) } } r := michi.NewRouter() r.Use(mid("a")) r.Handle("/a/", h("a")) r.Route("/b/", func(r *michi.Router) { r.Use(mid("b")) r.Handle("/", h("b")) r.With(mid("c1")).Handle("/c1/", h("c1")) r.Group(func(r *michi.Router) { r.Use(mid("c2")) r.Handle("/c2/", h("c2")) }) }) { w := httptest.NewRecorder() target := "https://example.com/a/" req := httptest.NewRequest(http.MethodPost, target, nil) fmt.Println(target) r.ServeHTTP(w, req) fmt.Println() } { w := httptest.NewRecorder() target := "https://example.com/b/" req := httptest.NewRequest(http.MethodPost, target, nil) fmt.Println(target) r.ServeHTTP(w, req) fmt.Println() } { w := httptest.NewRecorder() target := "https://example.com/b/c1/" req := httptest.NewRequest(http.MethodPost, target, nil) fmt.Println(target) r.ServeHTTP(w, req) fmt.Println() } { w := httptest.NewRecorder() target := "https://example.com/b/c2/" req := httptest.NewRequest(http.MethodPost, target, nil) fmt.Println(target) r.ServeHTTP(w, req) fmt.Println() } }
Output: https://example.com/a/ a start a handler a end https://example.com/b/ a start b start b handler b end a end https://example.com/b/c1/ a start b start c1 start c1 handler c1 end b end a end https://example.com/b/c2/ a start b start c2 start c2 handler c2 end b end a end
Index ¶
- type Router
- func (r *Router) Group(fn func(sub *Router))
- func (r *Router) Handle(pattern string, handler http.Handler)
- func (r *Router) HandleFunc(pattern string, handlerFunc http.HandlerFunc)
- func (r *Router) Route(pattern string, fn func(sub *Router))
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Use(middlewares ...func(http.Handler) http.Handler)
- func (r *Router) With(middlewares ...func(http.Handler) http.Handler) *Router
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is a http.Handler
func (*Router) Group ¶
Group creates a new inline-Mux with a copy of middleware stack. It's useful for a group of handlers along the same routing path that use an additional
func (*Router) Handle ¶
Handle adds the route `pattern` that matches any http method to execute the `handler` http.Handler.
func (*Router) HandleFunc ¶
func (r *Router) HandleFunc(pattern string, handlerFunc http.HandlerFunc)
HandleFunc adds the route `pattern` that matches any http method to execute the `handlerFn` http.HandlerFunc.
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP is the single method of the http.Handler interface that makes Mux interoperable with the standard library.
func (*Router) Use ¶
Use appends a middleware handler to the Mux middleware stack.
The middleware stack for any Mux will execute before searching for a matching route to a specific handler, which provides opportunity to respond early, change the course of the request execution, or set request-scoped values for the next http.Handler.