gorouter
v1.2.0
Documentation
pkg.go.dev ↗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_]+}.
Variables
ErrGenerateParameters (4 declarations)
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")
)
Functions
GetAllParams
func GetAllParams(r *http.Request) paramsMapType
GetAllParams returns all route params stored in http.Request.
GetParam
func GetParam(r *http.Request, key string) string
GetParam returns route param stored in http.request.
Types
MiddlewareType
type MiddlewareType func(next http.HandlerFunc) http.HandlerFunc
MiddlewareType is a public type that is used for middleware
Node
type Node struct {
// contains filtered or unexported fields
}
Node records any URL params, and executes an end handler.
NewNode
func NewNode(key string, depth int) *Node
NewNode returns a newly initialized Node object that implements the Node
Parameters
type Parameters struct {
// contains filtered or unexported fields
}
Parameters records some parameters
Router
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.
New
func New() *Router
New returns a newly initialized Router object that implements the Router
Router.DELETE
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.
Router.DELETEAndName
func (r *Router) DELETEAndName(path string, handle http.HandlerFunc, routeName string)
DELETEAndName is short for `DELETE` and Named routeName
Router.GET
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.
Router.GETAndName
func (r *Router) GETAndName(path string, handle http.HandlerFunc, routeName string)
GETAndName is short for `GET` and Named routeName
Router.Generate
func (r *Router) Generate(method string, routeName string, params map[string]string) (string, error)
Generate returns reverse routing by method, routeName and params
Router.Group
func (r *Router) Group(prefix string) *Router
Group define routes groups if there is a path prefix that uses `prefix`
Router.Handle
func (r *Router) Handle(method string, path string, handle http.HandlerFunc)
Handle registers a new request handler with the given path and method.
Router.HandleNotFound
func (r *Router) HandleNotFound(w http.ResponseWriter, req *http.Request, middleware []MiddlewareType)
HandleNotFound registers a handler when the request route is not found
Router.Match
func (r *Router) Match(requestUrl string, path string) bool
Match checks if the request matches the route pattern
Router.NotFoundFunc
func (r *Router) NotFoundFunc(handler http.HandlerFunc)
NotFoundFunc registers a handler when the request route is not found
Router.PATCH
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.
Router.PATCHAndName
func (r *Router) PATCHAndName(path string, handle http.HandlerFunc, routeName string)
PATCHAndName is short for `PATCH` and Named routeName
Router.POST
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.
Router.POSTAndName
func (r *Router) POSTAndName(path string, handle http.HandlerFunc, routeName string)
POSTAndName is short for `POST` and Named routeName
Router.PUT
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.
Router.PUTAndName
func (r *Router) PUTAndName(path string, handle http.HandlerFunc, routeName string)
PUTAndName is short for `PUT` and Named routeName
Router.ServeHTTP
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP makes the router implement the http.Handler interface.
Router.Use
func (r *Router) Use(middleware ...MiddlewareType)
Use appends a middleware handler to the middleware stack.
Tree
type Tree struct {
// contains filtered or unexported fields
}
Tree records node
NewTree
func NewTree() *Tree
NewTree returns a newly initialized Tree object that implements the Tree
Tree.Add
func (t *Tree) Add(pattern string, handle http.HandlerFunc, middleware ...MiddlewareType)
Add use `pattern` 、handle、middleware stack as node register to tree
Tree.Find
func (t *Tree) Find(pattern string, isRegex bool) (nodes []*Node)
Find returns nodes that the request match the route pattern