forked from GuilhermeCaruso/bellt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbellt.go
428 lines (357 loc) · 10.6 KB
/
bellt.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright 2019 Guilherme Caruso. All rights reserved.
// Use of this source code is governed by a MIT License
// license that can be found in the LICENSE file.
package bellt
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"regexp"
"strings"
)
var (
// Methods used for validation and comparison of the HandleFunc and
// Modules functions.
methods = []string{
"GET",
"POST",
"PUT",
"DELETE",
}
mainRouter *Router
)
// Router is a struct responsible for storing routes already available (Route)
// or routes that will still be available (BuiltRoute).
type Router struct {
routes []*Route
built []*BuiltRoute
}
// Route is a struct responsible for storing basic information of a Route, with
// all its variable parameters recorded.
type Route struct {
Path string
Handler http.HandlerFunc
Params []Variable
}
// SubHandle is a struct similar to Route, however its behavior must be related
// to GroupHandle, having all its behavior mirrored from a Route.
type SubHandle struct {
Path string
Handler http.HandlerFunc
Methods []string
}
// BuiltRoute is an internal pattern struct for routes that will be built at
// run time.
type BuiltRoute struct {
TempPath string
Handler http.HandlerFunc
Var map[int]Variable
KeyRoute string
Methods []string
}
// Variable is a struct that guarantees the correct mapping of variables used
// in built routes.
type Variable struct {
Name string
Value string
}
// ParamReceiver is responsible to return params set on context
type ParamReceiver struct {
request *http.Request
}
// Middleware is a type responsible for characterizing middleware functions
// that should be used in conjunction with bellt.Use().
type Middleware func(http.HandlerFunc) http.HandlerFunc
// Key is a type responsible for define a requester key param
type key string
// NewRouter is responsible to initialize a "singleton" router instance.
func NewRouter() *Router {
if mainRouter == nil {
http.HandleFunc("/health", healthApplication)
http.HandleFunc("/", redirectBuiltRoute)
mainRouter = &Router{}
}
return mainRouter
}
/*
Router is a struct responsible for storing routes already available (Route)
or routes that will still be available (BuiltRoute).
Its initialization is done through the method NewRouter:
router: = bellt.NewRouter ()
func main () {
[...]
log.Fatal (http.ListenAndServe (": 8080", nil))
}
*/
// Method to obtain router for interanl processing
func getRouter() *Router {
return mainRouter
}
// RedirectBuiltRoute Performs code analysis assigning values to variables
// in execution time.
func redirectBuiltRoute(w http.ResponseWriter, r *http.Request) {
selectedBuilt, params := getRequestParams(r.URL.Path)
if selectedBuilt != nil {
router := getRouter()
for idx, varParam := range selectedBuilt.Var {
selectedBuilt.Var[idx] = Variable{
Name: varParam.Name,
Value: params[idx],
}
}
var allParams []Variable
for _, param := range selectedBuilt.Var {
allParams = append(allParams, param)
}
router.createBuiltRoute(
selectedBuilt.TempPath,
selectedBuilt.Handler,
selectedBuilt.Methods,
selectedBuilt.Var)
setRouteParams(gateMethod(
selectedBuilt.Handler,
selectedBuilt.Methods...),
allParams).ServeHTTP(w, r)
} else {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"msg": "route not found"}`))
}
}
// Use becomes responsible for executing all middlewares passed through a
// cascade method.
func Use(handler http.HandlerFunc, middleware ...Middleware) http.HandlerFunc {
for x := len(middleware) - 1; x >= 0; x-- {
mid := middleware[x]
handler = mid(handler)
}
return handler
}
/*
The Use application should be done as follows within the
router.HandleFunc() method:
router: = bellt.NewRouter ()
func main () {
router.HandleFunc("path/", bellt.Use(
handlerFunc,
middleware1,
middleware2,
...,
), "GET")
log.Fatal (http.ListenAndServe (": 8080", nil))
}
*/
// ----------------------------------------------------------------------------
// Router methods
// ----------------------------------------------------------------------------
// HandleFunc function responsible for initializing a common route or built
// through the Router. All non-grouped routes must be initialized by this
// method.
func (r *Router) HandleFunc(path string, handleFunc http.HandlerFunc, methods ...string) {
key, values := getBuiltRouteParams(path)
if values != nil {
valuesList := make(map[int]Variable)
for idx, name := range values {
valuesList[idx] = Variable{
Name: name[1],
Value: "",
}
}
builtRoute := &BuiltRoute{
TempPath: path,
Handler: handleFunc,
Var: valuesList,
KeyRoute: key,
Methods: methods,
}
r.built = append(r.built, builtRoute)
} else {
route := &Route{
Path: path,
Handler: handleFunc,
}
err := route.methods(methods...)
if err == nil {
r.routes = append(r.routes, route)
}
}
}
/*
The HandleFunc application should be done as follows within the
router.HandleFunc() method:
router: = bellt.NewRouter ()
func main () {
router.HandleFunc("path/", bellt.Use(
handlerFunc,
middleware1,
middleware2,
...,
), "GET")
log.Fatal (http.ListenAndServe (": 8080", nil))
}
*/
// HandleGroup used to create and define a group of sub-routes
func (r *Router) HandleGroup(mainPath string, sr ...*SubHandle) {
for _, route := range sr {
var buf bytes.Buffer
buf.WriteString(mainPath)
buf.WriteString(route.Path)
r.HandleFunc(buf.String(), route.Handler, route.Methods...)
}
}
// SubHandleFunc is responsible for initializing a common or built route. Its
// use must be made within the scope of the HandleGroup() method, where the
// main path will be declared.
func (r *Router) SubHandleFunc(path string, handleFunc http.HandlerFunc,
methods ...string) *SubHandle {
handleDetail := &SubHandle{
Handler: handleFunc,
Path: path,
Methods: methods,
}
return handleDetail
}
// Internal method of route construction based on parameters passed in the
// HandleFunc, guaranteeing a valid and functional route.
func (r *Router) routeBuilder(path string, handleFunc http.HandlerFunc,
params ...Variable) *Route {
route := &Route{
Handler: handleFunc,
Path: path,
Params: params,
}
r.routes = append(r.routes, route)
return route
}
// Internal method responsible for standardizing built routes in order to
// generate valid models of used.
func (r *Router) createBuiltRoute(path string, handler http.HandlerFunc,
methods []string, params map[int]Variable) {
var (
builtPath = path
allParams []Variable
)
for _, param := range params {
builtPath = strings.Replace(builtPath, "{"+param.Name+"}",
param.Value, -1)
allParams = append(allParams, param)
}
r.routeBuilder(builtPath, handler, allParams...).methods(methods...)
}
// ----------------------------------------------------------------------------
// Route methods
// ----------------------------------------------------------------------------
// Internal method responsible for validating if the request method used exists
// for the route presented.
func (r *Route) methods(methods ...string) (err error) {
for _, method := range methods {
if !checkMethod(method) {
msgErro := fmt.Sprintf("Method %s on %s not allowed",
method, r.Path)
err = errors.New(msgErro)
}
}
if err == nil {
if len(r.Params) > 0 {
http.HandleFunc(r.Path,
setRouteParams(gateMethod(r.Handler, methods...), r.Params))
} else {
http.HandleFunc(r.Path, gateMethod(r.Handler,
methods...))
}
}
return
}
// Internal method that validates whether the value passed in methods matches
// existing values.
func checkMethod(m string) bool {
for _, method := range methods {
if m == method {
return true
}
}
return false
}
// ----------------------------------------------------------------------------
// Router middlewares
// ----------------------------------------------------------------------------
// Ensures that routing is done using valid methods
func gateMethod(next http.HandlerFunc, methods ...string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
for _, method := range methods {
if r.Method == method {
next.ServeHTTP(w, r)
return
}
}
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"error": "The method for this route doesnt exist"}`))
}
}
// Method to obtain route params in a built route
func getBuiltRouteParams(path string) (string, [][]string) {
rgx := regexp.MustCompile(`(?m){(\w*)}`)
rgxStart := regexp.MustCompile(`(?m)(^\/)`)
rgxEnd := regexp.MustCompile(`(?m)(\/$)`)
return rgxEnd.ReplaceAllString(rgxStart.ReplaceAllString(
rgx.Split(path, -1)[0], ""), ""), rgx.FindAllStringSubmatch(path, -1)
}
// Method to obtain request methods
func getRequestParams(path string) (*BuiltRoute, map[int]string) {
router := getRouter()
var builtRouteList *BuiltRoute
params := make(map[int]string)
for _, route := range router.built {
rgx := regexp.MustCompile(route.KeyRoute)
if rgx.FindString(path) != "" {
if (len(strings.Split(
rgx.Split(path, -1)[1], "/")) - 1) == len(route.Var) {
builtRouteList = route
for idx, val := range strings.Split(rgx.Split(path, -1)[1],
"/") {
if idx != 0 {
params[idx-1] = val
}
}
}
}
}
return builtRouteList, params
}
// RouteVariables used to capture and store parameters passed to built routes
func RouteVariables(r *http.Request) *ParamReceiver {
receiver := ParamReceiver{
request: r,
}
return &receiver
}
// Defines and organizes route parameters by applying them in request
func setRouteParams(next http.HandlerFunc, params []Variable) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
for _, param := range params {
name := key(param.Name)
ctx = context.WithValue(ctx, name, param.Value)
}
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
}
}
// ----------------------------------------------------------------------------
// ParamReceiver middlewares
// ----------------------------------------------------------------------------
// GetVar return a value of router variable
func (pr *ParamReceiver) GetVar(variable string) interface{} {
return pr.request.Context().Value(key(variable))
}
// ----------------------------------------------------------------------------
// Server support methods
// ----------------------------------------------------------------------------
// Function used in application health routing.
func healthApplication(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"alive": "Server running"}`))
}