Documentation
¶
Overview ¶
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 ¶
- Variables
- func GetAllParams(r *http.Request) paramsMapType
- func GetParam(r *http.Request, key string) string
- type MiddlewareType
- type Node
- type Parameters
- type Router
- func (r *Router) DELETE(path string, handle http.HandlerFunc)
- func (r *Router) DELETEAndName(path string, handle http.HandlerFunc, routeName string)
- func (r *Router) GET(path string, handle http.HandlerFunc)
- func (r *Router) GETAndName(path string, handle http.HandlerFunc, routeName string)
- func (r *Router) Generate(method string, routeName string, params map[string]string) (string, error)
- func (r *Router) Group(prefix string) *Router
- func (r *Router) Handle(method string, path string, handle http.HandlerFunc)
- func (r *Router) HandleNotFound(w http.ResponseWriter, req *http.Request, middleware []MiddlewareType)
- func (r *Router) Match(requestUrl string, path string) bool
- func (r *Router) NotFoundFunc(handler http.HandlerFunc)
- func (r *Router) PATCH(path string, handle http.HandlerFunc)
- func (r *Router) PATCHAndName(path string, handle http.HandlerFunc, routeName string)
- func (r *Router) POST(path string, handle http.HandlerFunc)
- func (r *Router) POSTAndName(path string, handle http.HandlerFunc, routeName string)
- func (r *Router) PUT(path string, handle http.HandlerFunc)
- func (r *Router) PUTAndName(path string, handle http.HandlerFunc, routeName string)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Use(middleware ...MiddlewareType)
- type Tree
Constants ¶
This section is empty.
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") )
Functions ¶
func GetAllParams ¶
GetAllParams returns all route params stored in http.Request.
Types ¶
type MiddlewareType ¶ added in v1.0.1
type MiddlewareType func(next http.HandlerFunc) http.HandlerFunc
MiddlewareType is a public type that is used for middleware
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node records any URL params, and executes an end handler.
type Parameters ¶ added in v1.1.0
type Parameters struct {
// contains filtered or unexported fields
}
Parameters records some parameters
type 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.
func New ¶
func New() *Router
New returns a newly initialized Router object that implements the Router
func (*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.
func (*Router) DELETEAndName ¶ added in v1.1.0
func (r *Router) DELETEAndName(path string, handle http.HandlerFunc, routeName string)
DELETEAndName is short for `DELETE` and Named routeName
func (*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.
func (*Router) GETAndName ¶ added in v1.1.0
func (r *Router) GETAndName(path string, handle http.HandlerFunc, routeName string)
GETAndName is short for `GET` and Named routeName
func (*Router) Generate ¶ added in v1.1.0
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) Handle ¶
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 ¶
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) NotFoundFunc ¶
func (r *Router) NotFoundFunc(handler http.HandlerFunc)
NotFoundFunc registers a handler when the request route is not found
func (*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.
func (*Router) PATCHAndName ¶ added in v1.1.0
func (r *Router) PATCHAndName(path string, handle http.HandlerFunc, routeName string)
PATCHAndName is short for `PATCH` and Named routeName
func (*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.
func (*Router) POSTAndName ¶ added in v1.1.0
func (r *Router) POSTAndName(path string, handle http.HandlerFunc, routeName string)
POSTAndName is short for `POST` and Named routeName
func (*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.
func (*Router) PUTAndName ¶ added in v1.1.0
func (r *Router) PUTAndName(path string, handle http.HandlerFunc, routeName string)
PUTAndName is short for `PUT` and Named routeName
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP makes the router implement the http.Handler interface.
func (*Router) Use ¶
func (r *Router) Use(middleware ...MiddlewareType)
Use appends a middleware handler to the middleware stack.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree records node
func NewTree ¶
func NewTree() *Tree
NewTree returns a newly initialized Tree object that implements the Tree
func (*Tree) Add ¶
func (t *Tree) Add(pattern string, handle http.HandlerFunc, middleware ...MiddlewareType)
Add use `pattern` 、handle、middleware stack as node register to tree
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
customNotFoundHandler
command
|
|
|
customPanicHandler
command
|
|
|
graceful
command
|
|
|
groupRoutes
command
|
|
|
helloworld
command
|
|
|
middlewares
command
|
|
|
reverseRouting
command
|
|
|
serveStaticFiles
command
|
|
|
urlParameters
command
|
|
|
urlRegex
command
|