Documentation ¶
Overview ¶
Package routing provides high performance and powerful HTTP routing capabilities.
Example ¶
router := routing.New() router.Use( // all these handlers are shared by every route access.Logger(log.Printf), slash.Remover(http.StatusMovedPermanently), fault.Recovery(log.Printf), ) // serve RESTful APIs api := router.Group("/api") api.Use( // these handlers are shared by the routes in the api group only content.TypeNegotiator(content.JSON, content.XML), ) api.Get("/users", func(c *routing.Context) error { return c.Write("user list") }) api.Post("/users", func(c *routing.Context) error { return c.Write("create a new user") }) api.Put(`/users/<id:\d+>`, func(c *routing.Context) error { return c.Write("update user " + c.Param("id")) }) // serve index file router.Get("/", file.Content("ui/index.html")) // serve files under the "ui" subdirectory router.Get("/*", file.Server(file.PathMap{ "/": "/ui/", })) http.Handle("/", router) http.ListenAndServe(":8080", nil)
Output:
Index ¶
- Variables
- func MethodNotAllowedHandler(c *Context) error
- func NotFoundHandler(*Context) error
- func Serialize(data interface{}) (bytes []byte, err error)
- type Context
- func (c *Context) Abort()
- func (c *Context) Get(name string) interface{}
- func (c *Context) Next() error
- func (c *Context) Param(name string) string
- func (c *Context) Router() *Router
- func (c *Context) Set(name string, value interface{})
- func (c *Context) URL(route string, pairs ...interface{}) string
- func (c *Context) WriteData(data interface{}) (err error)
- type HTTPError
- type Handler
- type Route
- func (r *Route) Connect(handlers ...Handler) *Route
- func (r *Route) Delete(handlers ...Handler) *Route
- func (r *Route) Get(handlers ...Handler) *Route
- func (r *Route) Head(handlers ...Handler) *Route
- func (r *Route) Name(domain string) *Route
- func (r *Route) Options(handlers ...Handler) *Route
- func (r *Route) Patch(handlers ...Handler) *Route
- func (r *Route) Post(handlers ...Handler) *Route
- func (r *Route) Put(handlers ...Handler) *Route
- func (r *Route) To(methods string, handlers ...Handler) *Route
- func (r *Route) Trace(handlers ...Handler) *Route
- func (r *Route) URL(pairs ...interface{}) (s string)
- type RouteGroup
- func (r *RouteGroup) Any(path string, handlers ...Handler) *Route
- func (r *RouteGroup) Connect(path string, handlers ...Handler) *Route
- func (r *RouteGroup) Delete(path string, handlers ...Handler) *Route
- func (r *RouteGroup) Get(path string, handlers ...Handler) *Route
- func (r *RouteGroup) Group(domain string, prefix string, handlers ...Handler) *RouteGroup
- func (r *RouteGroup) Head(path string, handlers ...Handler) *Route
- func (r *RouteGroup) Options(path string, handlers ...Handler) *Route
- func (r *RouteGroup) Patch(path string, handlers ...Handler) *Route
- func (r *RouteGroup) Post(path string, handlers ...Handler) *Route
- func (r *RouteGroup) Put(path string, handlers ...Handler) *Route
- func (r *RouteGroup) To(methods, path string, handlers ...Handler) *Route
- func (r *RouteGroup) Trace(path string, handlers ...Handler) *Route
- func (r *RouteGroup) Use(handlers ...Handler)
- type Router
- type SerializeFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Methods = []string{
"CONNECT",
"DELETE",
"GET",
"HEAD",
"OPTIONS",
"PATCH",
"POST",
"PUT",
"TRACE",
}
Methods lists all supported HTTP methods by Router.
Functions ¶
func MethodNotAllowedHandler ¶
MethodNotAllowedHandler handles the situation when a request has matching route without matching HTTP method. In this case, the handler will respond with an Allow HTTP header listing the allowed HTTP methods. Otherwise, the handler will do nothing and let the next handler (usually a NotFoundHandler) to handle the problem.
func NotFoundHandler ¶
NotFoundHandler returns a 404 HTTP error indicating a request has no matching route.
Types ¶
type Context ¶
type Context struct { *fasthttp.RequestCtx Serialize SerializeFunc // the function serializing the given data of arbitrary type into a byte array. // contains filtered or unexported fields }
Context represents the contextual data and environment while processing an incoming HTTP request.
func (*Context) Abort ¶
func (c *Context) Abort()
Abort skips the rest of the handlers associated with the current route. Abort is normally used when a handler handles the request normally and wants to skip the rest of the handlers. If a handler wants to indicate an error condition, it should simply return the error without calling Abort.
func (*Context) Get ¶
Get returns the named data item previously registered with the context by calling Set. If the named data item cannot be found, nil will be returned.
func (*Context) Next ¶
Next calls the rest of the handlers associated with the current route. If any of these handlers returns an error, Next will return the error and skip the following handlers. Next is normally used when a handler needs to do some postprocessing after the rest of the handlers are executed.
func (*Context) Param ¶
Param returns the named parameter value that is found in the URL path matching the current route. If the named parameter cannot be found, an empty string will be returned.
func (*Context) Set ¶
Set stores the named data item in the context so that it can be retrieved later.
func (*Context) URL ¶
URL creates a URL using the named route and the parameter values. The parameters should be given in the sequence of name1, value1, name2, value2, and so on. If a parameter in the route is not provided a value, the parameter token will remain in the resulting URL. Parameter values will be properly URL encoded. The method returns an empty string if the URL creation fails.
type HTTPError ¶
type HTTPError interface { error // StatusCode returns the HTTP status code of the error StatusCode() int }
HTTPError represents an HTTP error with HTTP status code and error message
func NewHTTPError ¶
NewHTTPError creates a new HttpError instance. If the error message is not given, http.StatusText() will be called to generate the message based on the status code.
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route represents a URL path pattern that can be used to match requested URLs.
func (*Route) Name ¶
Name sets the name of the route. This method will update the registration of the route in the router as well.
func (*Route) To ¶
To adds the route to the router with the given HTTP methods and handlers. Multiple HTTP methods should be separated by commas (without any surrounding spaces).
func (*Route) URL ¶
URL creates a URL using the current route and the given parameters. The parameters should be given in the sequence of name1, value1, name2, value2, and so on. If a parameter in the route is not provided a value, the parameter token will remain in the resulting URL. The method will perform URL encoding for all given parameter values.
type RouteGroup ¶
type RouteGroup struct {
// contains filtered or unexported fields
}
RouteGroup represents a group of routes that share the same path prefix.
func (*RouteGroup) Any ¶
func (r *RouteGroup) Any(path string, handlers ...Handler) *Route
Any adds a route with the given route, handlers, and the HTTP methods as listed in routing.Methods.
func (*RouteGroup) Connect ¶
func (r *RouteGroup) Connect(path string, handlers ...Handler) *Route
Connect adds a CONNECT route to the router with the given route path and handlers.
func (*RouteGroup) Delete ¶
func (r *RouteGroup) Delete(path string, handlers ...Handler) *Route
Delete adds a DELETE route to the router with the given route path and handlers.
func (*RouteGroup) Get ¶
func (r *RouteGroup) Get(path string, handlers ...Handler) *Route
Get adds a GET route to the router with the given route path and handlers.
func (*RouteGroup) Group ¶
func (r *RouteGroup) Group(domain string, prefix string, handlers ...Handler) *RouteGroup
Group creates a RouteGroup with the given route path prefix and handlers. The new group will combine the existing path prefix with the new one. If no handler is provided, the new group will inherit the handlers registered with the current group.
func (*RouteGroup) Head ¶
func (r *RouteGroup) Head(path string, handlers ...Handler) *Route
Head adds a HEAD route to the router with the given route path and handlers.
func (*RouteGroup) Options ¶
func (r *RouteGroup) Options(path string, handlers ...Handler) *Route
Options adds an OPTIONS route to the router with the given route path and handlers.
func (*RouteGroup) Patch ¶
func (r *RouteGroup) Patch(path string, handlers ...Handler) *Route
Patch adds a PATCH route to the router with the given route path and handlers.
func (*RouteGroup) Post ¶
func (r *RouteGroup) Post(path string, handlers ...Handler) *Route
Post adds a POST route to the router with the given route path and handlers.
func (*RouteGroup) Put ¶
func (r *RouteGroup) Put(path string, handlers ...Handler) *Route
Put adds a PUT route to the router with the given route path and handlers.
func (*RouteGroup) To ¶
func (r *RouteGroup) To(methods, path string, handlers ...Handler) *Route
To adds a route to the router with the given HTTP methods, route path, and handlers. Multiple HTTP methods should be separated by commas (without any surrounding spaces).
func (*RouteGroup) Trace ¶
func (r *RouteGroup) Trace(path string, handlers ...Handler) *Route
Trace adds a TRACE route to the router with the given route path and handlers.
func (*RouteGroup) Use ¶
func (r *RouteGroup) Use(handlers ...Handler)
Use registers one or multiple handlers to the current route group. These handlers will be shared by all routes belong to this group and its subgroups.
type Router ¶
type Router struct { RouteGroup // contains filtered or unexported fields }
Router manages routes and dispatches HTTP requests to the handlers of the matching routes.
func (*Router) HandleRequest ¶
func (r *Router) HandleRequest(ctx *fasthttp.RequestCtx)
HandleRequest handles the HTTP request.
func (*Router) NotFound ¶
NotFound specifies the handlers that should be invoked when the router cannot find any route matching a request. Note that the handlers registered via Use will be invoked first in this case.
type SerializeFunc ¶
SerializeFunc serializes the given data of arbitrary type into a byte array.