Documentation ¶
Overview ¶
Tinyrouter is Go http router supporting custom parameters in paths. The implementation contains only 500 lines of code.
Example ¶
package main import ( "fmt" "log" "net/http" tiny "github.com/go101/tinyrouter" ) func main() { routes := []tiny.Route{ { Method: "GET", Pattern: "/a/b/:c", HandleFunc: func(w http.ResponseWriter, req *http.Request) { params := tiny.PathParams(req) fmt.Fprintln(w, "/a/b/:c", "c =", params.Value("c")) }, }, { Method: "GET", Pattern: "/a/:b/c", HandleFunc: func(w http.ResponseWriter, req *http.Request) { params := tiny.PathParams(req) fmt.Fprintln(w, "/a/:b/c", "b =", params.Value("b")) }, }, { Method: "GET", Pattern: "/a/:b/:c", HandleFunc: func(w http.ResponseWriter, req *http.Request) { params := tiny.PathParams(req) fmt.Fprintln(w, "/a/:b/:c", "b =", params.Value("b"), "c =", params.Value("c")) }, }, { Method: "GET", Pattern: "/:a/b/c", HandleFunc: func(w http.ResponseWriter, req *http.Request) { params := tiny.PathParams(req) fmt.Fprintln(w, "/:a/b/c", "a =", params.Value("a")) }, }, { Method: "GET", Pattern: "/:a/:b/:c", HandleFunc: func(w http.ResponseWriter, req *http.Request) { params := tiny.PathParams(req) fmt.Fprintln(w, "/:a/:b/:c", "a =", params.Value("a"), "b =", params.Value("b"), "c =", params.Value("c")) }, }, } router := tiny.New(tiny.Config{Routes: routes}) log.Println("Starting service ...") log.Fatal(http.ListenAndServe(":8080", router)) /* $ curl localhost:8080/a/b/c /a/b/:c c = c $ curl localhost:8080/a/x/c /a/:b/c b = x $ curl localhost:8080/a/x/y /a/:b/:c b = x c = y $ curl localhost:8080/x/b/c /:a/b/c a = x $ curl localhost:8080/x/y/z /:a/:b/:c a = x b = y c = z */ }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // This routing table Routes []Route // Handler function for unmatched paths. // Nil means http.NotFound. OthersHandleFunc http.HandlerFunc }
A Config value specifies the properties of a TinyRouter.
type Params ¶
type Params struct {
// contains filtered or unexported fields
}
A Params encapsulates the parameters in request URL path.
func PathParams ¶
PathParams returns parameters passed from URL path.
func (Params) ToMapAndSlice ¶
Convert a Params to a map[string]string and a []string. Mainly for debug purpose.
func (Params) Value ¶
Value returns the parameter value corresponds to key. This method will never panic.
func (Params) ValueByIndex ¶
ValueByIndex returns the parameter value corresponds to index i. This method will never panic.
type Route ¶
type Route struct {
Method, Pattern string
HandleFunc http.HandlerFunc
}
A Route value specifies a request method, path pattern and the corresponding http handler function.
type TinyRouter ¶
type TinyRouter struct {
// contains filtered or unexported fields
}
func New ¶
func New(c Config) *TinyRouter
New returns a *TinyRouter value, which is also a http.Handler value.
func (*TinyRouter) DumpInfo ¶
func (tr *TinyRouter) DumpInfo() string
DumpInfo is for debug purpose.
func (*TinyRouter) ServeHTTP ¶
func (tr *TinyRouter) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP lets *TinyRouter implement http.Handler interface.