tinyrouter: github.com/go101/tinyrouter Index | Examples | Files

package tinyrouter

import "github.com/go101/tinyrouter"

Tinyrouter is Go http router supporting custom parameters in paths. The implementation contains only 500 lines of code.

Code:

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
*/

Index

Examples

Package Files

tinyrouter.go

type Config Uses

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 Uses

type Params struct {
    // contains filtered or unexported fields
}

A Params encapsulates the parameters in request URL path.

func PathParams Uses

func PathParams(req *http.Request) Params

PathParams returns parameters passed from URL path.

func (Params) ToMapAndSlice Uses

func (p Params) ToMapAndSlice() (kvs map[string]string, vs []string)

Convert a Params to a map[string]string and a []string. Mainly for debug purpose.

func (Params) Value Uses

func (p Params) Value(key string) string

Value returns the parameter value corresponds to key. This method will never panic.

func (Params) ValueByIndex Uses

func (p Params) ValueByIndex(i int) string

ValueByIndex returns the parameter value corresponds to index i. This method will never panic.

type Route Uses

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 Uses

type TinyRouter struct {
    // contains filtered or unexported fields
}

func New Uses

func New(c Config) *TinyRouter

New returns a *TinyRouter value, which is also a http.Handler value.

func (*TinyRouter) DumpInfo Uses

func (tr *TinyRouter) DumpInfo() string

DumpInfo is for debug purpose.

func (*TinyRouter) ServeHTTP Uses

func (tr *TinyRouter) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP lets *TinyRouter implement http.Handler interface.

Package tinyrouter imports 6 packages (graph) and is imported by 1 packages. Updated 2018-11-14. Refresh now. Tools for package owners.