-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
router.go
249 lines (221 loc) · 6.7 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package minima
/**
* Minima is a free and open source software under Mit license
Copyright (c) 2024 gominima
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* Authors @apoorvcodes @megatank58
* Maintainers @Panquesito7 @savioxavier @Shubhaankar-Sharma @apoorvcodes @megatank58
* Thank you for showing interest in minima and for this beautiful community
*/
import (
"fmt"
"net/http"
)
type Handler func(res *Response, req *Request)
/**
* @info The cache routes struct
* @property {string} [method] The route method
* @property {Handler} [handler] The handler for the cached route
* @property {string} [path] The path of the cached route
*/
type cacheRoute struct {
method string
path string
handler Handler
}
/**
* @info The router structure
* @property {map[string][]*tree} [routes] The radix-tree based routes
* @property {Handler} [notfound] The handler for the non matching routes
* @property {[]Handler} [minmiddleware] The minima handler middleware stack
* @property {[]func(http.Handler)http.Handler} [middleware] The http.Handler middleware stack
* @property {bool} [isCache] Whether the router is cache or not
* @property {[]*cacheRoute} [cacheRoute] Slice of cached routes
* @property {http.Handler} [handler] The single http.Handler built on chaining the whole middleware stack
*/
type Router struct {
notfound http.Handler
handler http.Handler
isCache bool
middlewares []func(http.Handler) http.Handler
cacheRoute []*cacheRoute
routes map[string]*tree
}
/*
*
- @info Make new default router interface
return {Router}
*/
func NewRouter() *Router {
r := &Router{
routes: map[string]*tree{
"GET": NewTree(),
"POST": NewTree(),
"PUT": NewTree(),
"DELETE": NewTree(),
"PATCH": NewTree(),
"OPTIONS": NewTree(),
"HEAD": NewTree(),
},
isCache: true,
notfound: nil,
middlewares: make([]func(http.Handler) http.Handler, 0),
cacheRoute: make([]*cacheRoute, 0),
}
return r
}
/*
*
- @info Registers a new route to router interface
- @param {string} [path] The route path
return {string, []string}
*/
func (r *Router) Register(method string, path string, handler Handler) error {
if r.handler == nil {
r.buildHandler()
}
if r.isCache {
r.cacheRoute = append(r.cacheRoute, &cacheRoute{
method: method,
path: path,
handler: handler,
})
return nil
}
routes, ok := r.routes[method]
if !ok {
return fmt.Errorf("method %s not valid", method)
}
routes.InsertNode(path, handler)
return nil
}
func (r *Router) NotFound(handler Handler) *Router {
r.notfound = buildHandler(handler, nil)
return r
}
/**
* @info Adds route with Get method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Router}
*/
func (r *Router) Get(path string, handler Handler) *Router {
r.Register("GET", path, handler)
return r
}
/**
* @info Adds route with Post method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Router}
*/
func (r *Router) Post(path string, handler Handler) *Router {
r.Register("POST", path, handler)
return r
}
/**
* @info Adds route with Put method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Router}
*/
func (r *Router) Put(path string, handler Handler) *Router {
r.Register("PUT", path, handler)
return r
}
/**
* @info Adds route with Patch method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Router}
*/
func (r *Router) Patch(path string, handler Handler) {
r.Register("PATCH", path, handler)
}
/**
* @info Adds route with Options method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Router}
*/
func (r *Router) Options(path string, handler Handler) *Router {
r.Register("OPTIONS", path, handler)
return r
}
/**
* @info Adds route with Head method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Router}
*/
func (r *Router) Head(path string, handler Handler) *Router {
r.Register("HEAD", path, handler)
return r
}
/**
* @info Adds route with Delete method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Router}
*/
func (r *Router) Delete(path string, handler Handler) *Router {
r.Register("DELETE", path, handler)
return r
}
/**
* @info Returns all the routes in router
* @returns {map[string][]*mux}
*/
func (r *Router) GetCacheRoutes() []*cacheRoute {
return r.cacheRoute
}
/**
* @info Appends all routes to core router instance
* @param {Router} [Router] The router instance to append
* @returns {Router}
*/
func (r *Router) UseRouter(Router *Router) {
routes := Router.GetCacheRoutes()
if !r.isCache {
for _, v := range routes {
err := r.Register(v.method, v.path, v.handler); if err != nil {
panic(err)
}
}
return
}
r.cacheRoute = append(r.cacheRoute, routes...)
}
/**
* @info Injects net/http middleware to the stack
* @param {...func(http.Handler)http.Handler} [handler] The handler stack to append
* @returns {}
*/
func (r *Router) use(handler ...func(http.Handler) http.Handler) {
if r.handler != nil {
panic("Minima: Middlewares can't go after the routes are mounted")
}
r.middlewares = append(r.middlewares, handler...)
}
// A dummy function that runs at the end of the middleware stack
func (r *Router) middlewareHTTP(w http.ResponseWriter, rq *http.Request) {}
/**
* @info Builds whole middleware stack chain into single handler
*/
func (r *Router) buildHandler() {
r.handler = chain(r.middlewares, http.HandlerFunc(r.middlewareHTTP))
}