Documentation ¶
Overview ¶
Package ginregex implements a regular expression router for the gin http framework.
Index ¶
- func Dispatch(matchers ...*Matcher) gin.HandlerFunc
- type Hook
- type Matcher
- type RegexRouter
- func (r *RegexRouter) Any(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
- func (r *RegexRouter) DELETE(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
- func (r *RegexRouter) GET(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
- func (r *RegexRouter) HEAD(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
- func (r *RegexRouter) Handle(httpMethod, regexPattern string, handlers ...gin.HandlerFunc) *RegexRouter
- func (r *RegexRouter) OPTIONS(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
- func (r *RegexRouter) PATCH(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
- func (r *RegexRouter) POST(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
- func (r *RegexRouter) PUT(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
- func (r *RegexRouter) Use(middleware ...gin.HandlerFunc) *RegexRouter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dispatch ¶ added in v0.1.1
func Dispatch(matchers ...*Matcher) gin.HandlerFunc
Dispatch can be used to dispatch requests to multiple different handlers under same path, using the given matchers. The matchers given order matters, if a path matches multiple matchers, only the first one (in the order they present) takes effect.
Compared to the `RegexRouter` in this package, `Dispatch` does not use unsafe tricks to hack the framework at runtime.
Usage example:
r := gin.Default() r.Any("/users/*any", ginregex.Dispatch( ginregex.NewMatcher("GET", `^/users/settings/$`, myMiddleware1, GetUserSettings), ginregex.NewMatcher("POST", `^/users/settings/$`, myMiddleware1, ModifyUserSettings), ginregex.NewMatcher("GET", `^/users/(?P<user_id>\d+)/$`, myMiddleware2, GetUserDetail), ))
Types ¶
type Matcher ¶ added in v0.1.1
type Matcher struct { Method string RE *regexp.Regexp Handlers gin.HandlersChain }
func NewMatcher ¶ added in v0.1.1
func NewMatcher(method, re string, handlers ...gin.HandlerFunc) *Matcher
NewMatcher returns a matcher to be used with the `Dispatch` function to dispatch requests using regular expressions. Named capturing will be filled into `gin.Context.Params`, you can access them like normal gin path parameters in your handler or binding functions.
The given re string must be a valid regular expression, else it will panic.
type RegexRouter ¶
type RegexRouter struct {
// contains filtered or unexported fields
}
RegexRouter is a regular expression router to be used with gin http framework. It uses unsafe magic to patch gin.Engine and gin.Context on the fly.
If named capturing with the (?P<name>...) syntax presents in registered routes, the captured values will be filled into gin.Context.Params, so you can access them like normal gin path parameters in your handlers or binding functions.
func New ¶
func New(engine *gin.Engine, hook Hook) *RegexRouter
New creates a regular expression router with the given gin.Engine.
To function properly, only one RegexRouter can be created for each gin.Engine pointer, if called multiple times using same gin.Engine pointer, it returns the RegexRouter previously created.
func (*RegexRouter) Any ¶
func (r *RegexRouter) Any(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
Any registers a route that matches all the HTTP methods. GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func (*RegexRouter) DELETE ¶
func (r *RegexRouter) DELETE(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
DELETE is a shortcut for router.Handle("DELETE", path, handle).
func (*RegexRouter) GET ¶
func (r *RegexRouter) GET(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
GET is a shortcut for router.Handle("GET", path, handle).
func (*RegexRouter) HEAD ¶
func (r *RegexRouter) HEAD(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
HEAD is a shortcut for router.Handle("HEAD", path, handle).
func (*RegexRouter) Handle ¶
func (r *RegexRouter) Handle(httpMethod, regexPattern string, handlers ...gin.HandlerFunc) *RegexRouter
Handle registers a new request handle and middleware with the given path and method. The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes. See the example code in GitHub.
For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.
This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).
If the regex pattern does not start with "^", "^" will be added to the start.
func (*RegexRouter) OPTIONS ¶
func (r *RegexRouter) OPTIONS(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle).
func (*RegexRouter) PATCH ¶
func (r *RegexRouter) PATCH(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
PATCH is a shortcut for router.Handle("PATCH", path, handle).
func (*RegexRouter) POST ¶
func (r *RegexRouter) POST(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
POST is a shortcut for router.Handle("POST", path, handle).
func (*RegexRouter) PUT ¶
func (r *RegexRouter) PUT(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter
PUT is a shortcut for router.Handle("PUT", path, handle).
func (*RegexRouter) Use ¶
func (r *RegexRouter) Use(middleware ...gin.HandlerFunc) *RegexRouter
Use adds middleware to the router.