-
Notifications
You must be signed in to change notification settings - Fork 4
/
exported.go
121 lines (106 loc) · 4.31 KB
/
exported.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package ginregex
import (
"fmt"
"github.com/gin-gonic/gin"
"strings"
)
var regexRouters = make(map[*gin.Engine]*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 New(engine *gin.Engine, hook Hook) *RegexRouter {
router, ok := regexRouters[engine]
if !ok {
initOffset()
router = &RegexRouter{
engine: engine,
hook: hook,
table: make(map[string][]*regexHandler),
}
router.patchEngine()
regexRouters[engine] = router
}
return router
}
type Hook func(c *gin.Context, regexPattern string)
// 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.
type RegexRouter struct {
engine *gin.Engine
hook Hook
handlers gin.HandlersChain
table map[string][]*regexHandler
}
// Use adds middleware to the router.
func (r *RegexRouter) Use(middleware ...gin.HandlerFunc) *RegexRouter {
r.handlers = append(r.handlers, middleware...)
return r
}
// 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 (r *RegexRouter) Handle(httpMethod, regexPattern string, handlers ...gin.HandlerFunc) *RegexRouter {
methodHandlers := r.table[httpMethod]
for _, h := range methodHandlers {
if strings.TrimPrefix(h.pattern, "^") ==
strings.TrimPrefix(regexPattern, "^") {
panic(fmt.Sprintf("register duplicate regex route: %s %s", httpMethod, regexPattern))
}
}
handler := newRegexHandler(regexPattern, handlers)
r.table[httpMethod] = append(methodHandlers, handler)
return r
}
// Any registers a route that matches all the HTTP methods.
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func (r *RegexRouter) Any(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter {
for _, method := range []string{"GET", "POST", "PUT", "PATCH", "HEAD", "OPTIONS", "DELETE", "CONNECT", "TRACE"} {
r.Handle(method, regexPath, handlers...)
}
return r
}
// GET is a shortcut for router.Handle("GET", path, handle).
func (r *RegexRouter) GET(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter {
return r.Handle("GET", regexPath, handlers...)
}
// POST is a shortcut for router.Handle("POST", path, handle).
func (r *RegexRouter) POST(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter {
return r.Handle("POST", regexPath, handlers...)
}
// DELETE is a shortcut for router.Handle("DELETE", path, handle).
func (r *RegexRouter) DELETE(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter {
return r.Handle("DELETE", regexPath, handlers...)
}
// PATCH is a shortcut for router.Handle("PATCH", path, handle).
func (r *RegexRouter) PATCH(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter {
return r.Handle("PATCH", regexPath, handlers...)
}
// PUT is a shortcut for router.Handle("PUT", path, handle).
func (r *RegexRouter) PUT(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter {
return r.Handle("PUT", regexPath, handlers...)
}
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle).
func (r *RegexRouter) OPTIONS(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter {
return r.Handle("OPTIONS", regexPath, handlers...)
}
// HEAD is a shortcut for router.Handle("HEAD", path, handle).
func (r *RegexRouter) HEAD(regexPath string, handlers ...gin.HandlerFunc) *RegexRouter {
return r.Handle("HEAD", regexPath, handlers...)
}