gorouter: github.com/xujiajun/gorouter Index | Files

package gorouter

import "github.com/xujiajun/gorouter"

Package gorouter is a simple and fast HTTP router for Go. It is easy to build RESTful APIs and your web framework.

Here is the example:

 package main

 import (
	 "log"
	 "net/http"

	 "github.com/xujiajun/gorouter"
 )

 func main() {
	 mux := gorouter.New()
	 //url parameters match
	 mux.GET("/user/{id:[0-9]+}", func(w http.ResponseWriter, r *http.Request) {
		//get one URL parameter
		id := gorouter.GetParam(r, "id")
		//get all URL parameters
		//id := gorouter.GetAllParams(r)
		//fmt.Println(id)
		w.Write([]byte("match user/{id:[0-9]+} ! get id:" + id))
	  })

	  log.Fatal(http.ListenAndServe(":8181", mux))
 }

Here is the syntax:

Syntax	                   Description	                          Example
:name	            named parameter	                        /user/:name
{name:regexp}	    named with regexp parameter	            /user/{name:[0-9a-zA-Z]+}
:id	                named with regexp parameter	            /user/:id

And :id is short for {id:[0-9]+}, :name are short for {name:[0-9a-zA-Z_]+}.

Index

Package Files

doc.go router.go tree.go

Variables

var (
    // ErrGenerateParameters is returned when generating a route with wrong parameters.
    ErrGenerateParameters = errors.New("params contains wrong parameters")

    // ErrNotFoundRoute is returned when generating a route that can not find route in tree.
    ErrNotFoundRoute = errors.New("cannot find route in tree")

    // ErrNotFoundMethod is returned when generating a route that can not find method in tree.
    ErrNotFoundMethod = errors.New("cannot find method in tree")

    // ErrPatternGrammar is returned when generating a route that pattern grammar error.
    ErrPatternGrammar = errors.New("pattern grammar error")
)

func GetAllParams Uses

func GetAllParams(r *http.Request) paramsMapType

GetAllParams returns all route params stored in http.Request.

func GetParam Uses

func GetParam(r *http.Request, key string) string

GetParam returns route param stored in http.request.

type MiddlewareType Uses

type MiddlewareType func(next http.HandlerFunc) http.HandlerFunc

MiddlewareType is a public type that is used for middleware

type Node Uses

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

Node records any URL params, and executes an end handler.

func NewNode Uses

func NewNode(key string, depth int) *Node

NewNode returns a newly initialized Node object that implements the Node

type Parameters Uses

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

Parameters records some parameters

type Router Uses

type Router struct {

    // PanicHandler for handling panic.
    PanicHandler func(w http.ResponseWriter, req *http.Request, err interface{})
    // contains filtered or unexported fields
}

Router is a simple HTTP route multiplexer that parses a request path, records any URL params, and executes an end handler.

func New Uses

func New() *Router

New returns a newly initialized Router object that implements the Router

func (*Router) DELETE Uses

func (r *Router) DELETE(path string, handle http.HandlerFunc)

DELETE adds the route `path` that matches a DELETE http method to execute the `handle` http.HandlerFunc.

func (*Router) DELETEAndName Uses

func (r *Router) DELETEAndName(path string, handle http.HandlerFunc, routeName string)

DELETEAndName is short for `DELETE` and Named routeName

func (*Router) GET Uses

func (r *Router) GET(path string, handle http.HandlerFunc)

GET adds the route `path` that matches a GET http method to execute the `handle` http.HandlerFunc.

func (*Router) GETAndName Uses

func (r *Router) GETAndName(path string, handle http.HandlerFunc, routeName string)

GETAndName is short for `GET` and Named routeName

func (*Router) Generate Uses

func (r *Router) Generate(method string, routeName string, params map[string]string) (string, error)

Generate returns reverse routing by method, routeName and params

func (*Router) Group Uses

func (r *Router) Group(prefix string) *Router

Group define routes groups if there is a path prefix that uses `prefix`

func (*Router) Handle Uses

func (r *Router) Handle(method string, path string, handle http.HandlerFunc)

Handle registers a new request handler with the given path and method.

func (*Router) HandleNotFound Uses

func (r *Router) HandleNotFound(w http.ResponseWriter, req *http.Request, middleware []MiddlewareType)

HandleNotFound registers a handler when the request route is not found

func (*Router) Match Uses

func (r *Router) Match(requestUrl string, path string) bool

Match checks if the request matches the route pattern

func (*Router) NotFoundFunc Uses

func (r *Router) NotFoundFunc(handler http.HandlerFunc)

NotFoundFunc registers a handler when the request route is not found

func (*Router) PATCH Uses

func (r *Router) PATCH(path string, handle http.HandlerFunc)

PATCH adds the route `path` that matches a PATCH http method to execute the `handle` http.HandlerFunc.

func (*Router) PATCHAndName Uses

func (r *Router) PATCHAndName(path string, handle http.HandlerFunc, routeName string)

PATCHAndName is short for `PATCH` and Named routeName

func (*Router) POST Uses

func (r *Router) POST(path string, handle http.HandlerFunc)

POST adds the route `path` that matches a POST http method to execute the `handle` http.HandlerFunc.

func (*Router) POSTAndName Uses

func (r *Router) POSTAndName(path string, handle http.HandlerFunc, routeName string)

POSTAndName is short for `POST` and Named routeName

func (*Router) PUT Uses

func (r *Router) PUT(path string, handle http.HandlerFunc)

PUT adds the route `path` that matches a PUT http method to execute the `handle` http.HandlerFunc.

func (*Router) PUTAndName Uses

func (r *Router) PUTAndName(path string, handle http.HandlerFunc, routeName string)

PUTAndName is short for `PUT` and Named routeName

func (*Router) ServeHTTP Uses

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP makes the router implement the http.Handler interface.

func (*Router) Use Uses

func (r *Router) Use(middleware ...MiddlewareType)

Use appends a middleware handler to the middleware stack.

type Tree Uses

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

Tree records node

func NewTree Uses

func NewTree() *Tree

NewTree returns a newly initialized Tree object that implements the Tree

func (*Tree) Add Uses

func (t *Tree) Add(pattern string, handle http.HandlerFunc, middleware ...MiddlewareType)

Add use `pattern` 、handle、middleware stack as node register to tree

func (*Tree) Find Uses

func (t *Tree) Find(pattern string, isRegex bool) (nodes []*Node)

Find returns nodes that the request match the route pattern

Package gorouter imports 6 packages (graph). Updated 2019-03-13. Refresh now. Tools for package owners.