-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.go
116 lines (102 loc) · 3.46 KB
/
routes.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
package goctapus
import (
"github.com/didip/tollbooth"
"github.com/didip/tollbooth/limiter"
"github.com/didip/tollbooth_echo"
"github.com/labstack/echo"
Log "github.com/sirupsen/logrus"
)
type Route struct {
Method string
Path string
Handler echo.HandlerFunc
File string
Rate float64
_limiter *limiter.Limiter
}
var Routes map[string]Route
func prependMiddlewareArray(arr []echo.MiddlewareFunc, item echo.MiddlewareFunc) []echo.MiddlewareFunc {
return append([]echo.MiddlewareFunc{item}, arr...)
}
func GET(routeInfo Route, m ...echo.MiddlewareFunc) {
//Check if Rate Limiter will be used for this route
if routeInfo.Rate > 0 {
// Register the route using the rate limiter middleware
Server.GET(routeInfo.Path, routeInfo.Handler, prependMiddlewareArray(m, tollbooth_echo.LimitHandler(routeInfo._limiter))...)
} else {
// Register the route without a rate limiter
Server.GET(routeInfo.Path, routeInfo.Handler, m...)
}
}
func POST(routeInfo Route, m ...echo.MiddlewareFunc) {
//Check if Rate Limiter will be used for this route
if routeInfo.Rate > 0 {
// Register the route using the rate limiter middleware
Server.POST(routeInfo.Path, routeInfo.Handler, prependMiddlewareArray(m, tollbooth_echo.LimitHandler(routeInfo._limiter))...)
} else {
// Register the route without a rate limiter
Server.POST(routeInfo.Path, routeInfo.Handler, m...)
}
}
func PUT(routeInfo Route, m ...echo.MiddlewareFunc) {
//Check if Rate Limiter will be used for this route
if routeInfo.Rate > 0 {
// Register the route using the rate limiter middleware
Server.PUT(routeInfo.Path, routeInfo.Handler, prependMiddlewareArray(m, tollbooth_echo.LimitHandler(routeInfo._limiter))...)
} else {
// Register the route without a rate limiter
Server.PUT(routeInfo.Path, routeInfo.Handler, m...)
}
}
func DELETE(routeInfo Route, m ...echo.MiddlewareFunc) {
//Check if Rate Limiter will be used for this route
if routeInfo.Rate > 0 {
// Register the route using the rate limiter middleware
Server.DELETE(routeInfo.Path, routeInfo.Handler, prependMiddlewareArray(m, tollbooth_echo.LimitHandler(routeInfo._limiter))...)
} else {
// Register the route without a rate limiter
Server.DELETE(routeInfo.Path, routeInfo.Handler, m...)
}
}
func AddStatic(routeInfo Route, m ...echo.MiddlewareFunc) {
//Check if Rate Limiter will be used for this route
if routeInfo.Rate > 0 {
// Register the route using the rate limiter middleware
Server.File(routeInfo.Path, routeInfo.File, prependMiddlewareArray(m, tollbooth_echo.LimitHandler(routeInfo._limiter))...)
} else {
// Register the route without a rate limiter
Server.File(routeInfo.Path, routeInfo.File, m...)
}
}
func AddEndpoint(routeInfo Route, m ...echo.MiddlewareFunc) {
//Generate route descriptor string
routeName := routeInfo.Method + ":" + routeInfo.Path
//Check if the route already exists
if _, ok := Routes[routeName]; ok {
// Route already exists
// Just return with a message
Log.Warning("Could not add endpoint " + routeName + ". Route already exists!")
return
}
// Route doesn't exist
// Create Rate Limiter if Rate value is set
if routeInfo.Rate > 0 {
routeInfo._limiter = tollbooth.NewLimiter(routeInfo.Rate, nil)
}
// Store route into map and create it
Routes[routeName] = routeInfo
switch routeInfo.Method {
case "GET":
GET(routeInfo, m...)
break
case "POST":
POST(routeInfo, m...)
break
case "PUT":
PUT(routeInfo, m...)
break
case "DELETE":
DELETE(routeInfo, m...)
break
}
}