-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
214 lines (177 loc) · 5.85 KB
/
router.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package router
import (
"fmt"
"log"
"net/http"
"strings"
"sync"
)
type Middleware func(http.HandlerFunc) http.HandlerFunc
func applyMiddlewares(handler http.HandlerFunc, middlewares ...Middleware) http.HandlerFunc {
for i := len(middlewares) - 1; i >= 0; i-- {
handler = middlewares[i](handler)
}
return handler
}
type Route struct {
Method string
Pattern string
HandlerFunc http.HandlerFunc
Middlewares []Middleware
}
func (r *Route) Use(middleware ...Middleware) *Route {
r.Middlewares = append(r.Middlewares, middleware...)
return r
}
type RouteGroup struct {
Prefix string
Middlewares []Middleware
Routes []*Route
}
func (rg *RouteGroup) Use(middleware ...Middleware) *RouteGroup {
rg.Middlewares = append(rg.Middlewares, middleware...)
return rg
}
type RouterConfig struct {
// DisableAutoAddExactMatchWildcard will disable the automatic addition of a wildcard to the end of a route pattern
// The router adds this by default, to prevent unexpected behavior as Go's pattern matching is a bit strange
DisableAutoAddExactMatchWildcard bool
// DisableAutoAddTrailingSlash will disable the automatic addition of a trailing slash to the end of a route pattern
// The router adds this by default, to prevent unexpected behavior as Go's pattern matching is a bit strange
DisableAutoAddTrailingSlash bool
}
type Router struct {
mutex sync.Mutex
mux *http.ServeMux
routes []*Route
routeGroups []*RouteGroup
middlewares []Middleware
hasSetupRoutes bool
config RouterConfig
}
func NewRouter() *Router {
return &Router{}
}
func (r *Router) SetMux(mux *http.ServeMux) {
r.mux = mux
}
func (r *Router) SetConfig(config RouterConfig) {
r.config = config
}
func (r *Router) RegisterRoute(method string, pattern string, handler http.HandlerFunc) *Route {
route := &Route{
Method: method,
Pattern: pattern,
HandlerFunc: handler,
Middlewares: nil,
}
r.routes = append(r.routes, route)
return route
}
func (r *Router) GET(pattern string, handler http.HandlerFunc) *Route {
return r.RegisterRoute(http.MethodGet, pattern, handler)
}
func (r *Router) POST(pattern string, handler http.HandlerFunc) *Route {
return r.RegisterRoute(http.MethodPost, pattern, handler)
}
func (r *Router) PUT(pattern string, handler http.HandlerFunc) *Route {
return r.RegisterRoute(http.MethodPut, pattern, handler)
}
func (r *Router) DELETE(pattern string, handler http.HandlerFunc) *Route {
return r.RegisterRoute(http.MethodDelete, pattern, handler)
}
func (r *Router) PATCH(pattern string, handler http.HandlerFunc) *Route {
return r.RegisterRoute(http.MethodPatch, pattern, handler)
}
func (r *Router) OPTIONS(pattern string, handler http.HandlerFunc) *Route {
return r.RegisterRoute(http.MethodOptions, pattern, handler)
}
func (r *Router) HEAD(pattern string, handler http.HandlerFunc) *Route {
return r.RegisterRoute(http.MethodHead, pattern, handler)
}
func (r *Router) CONNECT(pattern string, handler http.HandlerFunc) *Route {
return r.RegisterRoute(http.MethodConnect, pattern, handler)
}
func (r *Router) TRACE(pattern string, handler http.HandlerFunc) *Route {
return r.RegisterRoute(http.MethodTrace, pattern, handler)
}
func (r *Router) Group(prefix string, group func(r *Router)) *RouteGroup {
tmpRouter := &Router{middlewares: make([]Middleware, len(r.middlewares))}
copy(tmpRouter.middlewares, r.middlewares)
group(tmpRouter)
rg := &RouteGroup{
Prefix: prefix,
Routes: tmpRouter.routes,
Middlewares: tmpRouter.middlewares,
}
r.routeGroups = append(r.routeGroups, rg)
return rg
}
func (r *Router) Use(middleware ...Middleware) {
r.middlewares = append(r.middlewares, middleware...)
}
func (r *Router) SanitizePath(path string) string {
for strings.Contains(path, "//") {
path = strings.Replace(path, "//", "/", -1)
}
if path[0] != '/' {
path = "/" + path
}
if !r.config.DisableAutoAddTrailingSlash && path[len(path)-1] != '/' {
path = path + "/"
}
if !r.config.DisableAutoAddExactMatchWildcard {
path = path + "{$}"
}
return path
}
func (r *Router) GetPathForRoute(route *Route) string {
path := fmt.Sprintf("/%s", route.Pattern)
return fmt.Sprintf("%s %s", route.Method, r.SanitizePath(path))
}
func (r *Router) GetPathForRouteWithRouteGroup(route *Route, routeGroup *RouteGroup) string {
path := fmt.Sprintf("/%s/%s", routeGroup.Prefix, route.Pattern)
return fmt.Sprintf("%s %s", route.Method, r.SanitizePath(path))
}
func (r *Router) SetupRoutes() {
if r.mux == nil {
log.Println("Warning: ServeMux is nil, creating a default one")
r.mux = http.NewServeMux()
}
// This function combines the global middlewares with the route middlewares and the route group middlewares
combineMiddlewares := func(routeMiddlewares []Middleware, globalMiddlewares []Middleware) []Middleware {
allMiddlewares := make([]Middleware, 0, len(globalMiddlewares)+len(routeMiddlewares))
allMiddlewares = append(allMiddlewares, globalMiddlewares...)
allMiddlewares = append(allMiddlewares, routeMiddlewares...)
return allMiddlewares
}
for _, route := range r.routes {
handler := applyMiddlewares(
route.HandlerFunc,
combineMiddlewares(route.Middlewares, r.middlewares)...,
)
r.mux.HandleFunc(r.GetPathForRoute(route), func(w http.ResponseWriter, req *http.Request) {
handler(w, req)
})
}
for _, routeGroup := range r.routeGroups {
for _, route := range routeGroup.Routes {
handler := applyMiddlewares(
route.HandlerFunc,
combineMiddlewares(append(routeGroup.Middlewares, route.Middlewares...), r.middlewares)...,
)
r.mux.HandleFunc(r.GetPathForRouteWithRouteGroup(route, routeGroup), func(w http.ResponseWriter, req *http.Request) {
handler(w, req)
})
}
}
}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !r.hasSetupRoutes {
r.mutex.Lock()
r.SetupRoutes()
r.hasSetupRoutes = true
r.mutex.Unlock()
}
r.mux.ServeHTTP(w, req)
}