Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/zeebo/hmux" ) func main() { handler := http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) { fmt.Printf("method: %q\n", req.Method) fmt.Printf("path: %q\n", req.URL.Path) fmt.Printf("name: %q\n", hmux.Arg("name").Value(req.Context())) }) resources := hmux.Dir{ "/foo": hmux.Dir{ "*": hmux.Arg("name").Capture( hmux.Method{ "GET": handler, "POST": handler, }, ), }, } resources.ServeHTTP(nil, httptest.NewRequest("POST", "/foo/bar", nil)) fmt.Println("---") resources.ServeHTTP(nil, httptest.NewRequest("GET", "/foo/baz/bif", nil)) }
Output: method: "POST" path: "" name: "bar" --- method: "GET" path: "/bif" name: "baz"
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Arg ¶
type Arg string
Arg captures path components and attaches them to the request context. It always captures a non-empty component.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/zeebo/hmux" ) func main() { resources := hmux.Arg("name").Capture( http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) { fmt.Printf("arg: %q\n", hmux.Arg("name").Value(req.Context())) fmt.Printf("path: %q\n", req.URL.Path) }), ) resources.ServeHTTP(nil, httptest.NewRequest("GET", "/foo/bar", nil)) }
Output: arg: "foo" path: "/bar"
type Dir ¶
Dir pulls off path components from the front of the path and dispatches. It attempts to dispatch to "*" without consuming a path component if nothing matches. To ensure a path ends, use a key of "". To ensure it ends with a slash, use "/".
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/zeebo/hmux" ) func main() { resources := hmux.Dir{ "/foo": http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) { fmt.Printf("path: %q\n", req.URL.Path) }), } resources.ServeHTTP(nil, httptest.NewRequest("GET", "/foo/bar", nil)) }
Output: path: "/bar"
type Method ¶
Method checks the request method and dispatches.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/zeebo/hmux" ) func main() { resources := hmux.Method{ "POST": http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) { fmt.Printf("method: %q\n", req.Method) fmt.Printf("path: %q\n", req.URL.Path) }), } resources.ServeHTTP(nil, httptest.NewRequest("POST", "/foo", nil)) }
Output: method: "POST" path: "/foo"
Click to show internal directories.
Click to hide internal directories.