Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/sonatard/michi" ) func main() { handler := func(name string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Println(name + " handler") }) } middleware := 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") }) } } rt := michi.NewRouter() rt.Use(middleware("a")) rt.Handle("/a", handler("a")) rt.Route("/b", func(rt michi.Router) { rt.Use(middleware("b")) rt.Handle("", handler("b")) rt.With(middleware("c1")).Handle("/c1", handler("c1")) rt.Group(func(rt michi.Router) { rt.Use(middleware("c2")) rt.Handle("/c2", handler("c2")) }) }) { w := httptest.NewRecorder() target := "https://example.com/a" r := httptest.NewRequest(http.MethodPost, target, nil) fmt.Println(target) rt.ServeHTTP(w, r) fmt.Println() } { w := httptest.NewRecorder() target := "https://example.com/b" r := httptest.NewRequest(http.MethodPost, target, nil) fmt.Println(target) rt.ServeHTTP(w, r) fmt.Println() } { w := httptest.NewRecorder() target := "https://example.com/b/c1" r := httptest.NewRequest(http.MethodPost, target, nil) fmt.Println(target) rt.ServeHTTP(w, r) fmt.Println() } { w := httptest.NewRecorder() target := "https://example.com/b/c2" r := httptest.NewRequest(http.MethodPost, target, nil) fmt.Println(target) rt.ServeHTTP(w, r) 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 ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*router)
Option is the option for router
func ExecTrailingSlashHandlerOption ¶
func ExecTrailingSlashHandlerOption() Option
ExecTrailingSlashHandlerOption is the option for router. Execute the handler when the url path has no trailing slash.
type Router ¶
type Router interface { // Handler has ServeHTTP(ResponseWriter, *Request) http.Handler // Use adds middlewares to the router Use(middlewares ...func(http.Handler) http.Handler) // With creates a sub router with the given middlewares With(middlewares ...func(http.Handler) http.Handler) Router // Group creates a sub router with the given sub router function Group(fn func(sub Router)) // Route creates a sub router with the given sub router function and path pattern Route(pattern string, fn func(sub Router)) // Handle registers a handler for the given pattern Handle(pattern string, handler http.Handler) // HandleFunc registers a handler function for the given pattern HandleFunc(pattern string, handlerFunc http.HandlerFunc) }
Router is a http.Handler which can be used to dispatch requests to different
Click to show internal directories.
Click to hide internal directories.