-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.go
130 lines (99 loc) · 3.67 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package route
import (
"time"
echo "github.com/datumforge/echox"
"github.com/datumforge/echox/middleware"
"github.com/getkin/kin-openapi/openapi3"
"github.com/datumforge/datum/pkg/middleware/ratelimit"
"github.com/datumforge/go-template/internal/httpserve/handlers"
"github.com/datumforge/go-template/pkg/middleware/transaction"
)
var (
mw = []echo.MiddlewareFunc{middleware.Recover()}
authMW = []echo.MiddlewareFunc{}
restrictedRateLimit = &ratelimit.Config{RateLimit: 10, BurstLimit: 10, ExpiresIn: 15 * time.Minute} //nolint:mnd
restrictedEndpointsMW = []echo.MiddlewareFunc{}
)
// Router is a struct that holds the echo router, the OpenAPI schema, and the handler - it's a way to group these components together
type Router struct {
Echo *echo.Echo
OAS *openapi3.T
Handler *handlers.Handler
}
// AddRoute is used to add a route to the echo router and OpenAPI schema at the same time ensuring consistency between the spec and the server
func (r *Router) AddRoute(pattern, method string, op *openapi3.Operation, route echo.Routable) error {
_, err := r.Echo.AddRoute(route)
if err != nil {
return err
}
r.OAS.AddOperation(pattern, method, op)
return nil
}
// AddRoute is used to add a route to the echo router and OpenAPI schema at the same time ensuring consistency between the spec and the server
func (r *Router) Addv1Route(pattern, method string, op *openapi3.Operation, route echo.Routable) error {
grp := r.VersionOne()
_, err := grp.AddRoute(route)
if err != nil {
return err
}
r.OAS.AddOperation(pattern, method, op)
return nil
}
// AddRoute is used to add a route to the echo router and OpenAPI schema at the same time ensuring consistency between the spec and the server
func (r *Router) AddUnversionedRoute(pattern, method string, op *openapi3.Operation, route echo.Routable) error {
grp := r.Base()
_, err := grp.AddRoute(route)
if err != nil {
return err
}
r.OAS.AddOperation(pattern, method, op)
return nil
}
// AddEchoOnlyRoute is used to add a route to the echo router without adding it to the OpenAPI schema
func (r *Router) AddEchoOnlyRoute(pattern, method string, route echo.Routable) error {
grp := r.Base()
_, err := grp.AddRoute(route)
if err != nil {
return err
}
return nil
}
// VersionOne returns a new echo group for version 1 of the API
func (r *Router) VersionOne() *echo.Group {
return r.Echo.Group("v1")
}
// VersionTwo returns a new echo group for version 2 of the API - lets anticipate the future
func (r *Router) VersionTwo() *echo.Group {
return r.Echo.Group("v2")
}
// Base returns the base echo group - no "version" prefix for the router group
func (r *Router) Base() *echo.Group {
return r.Echo.Group("")
}
// RegisterRoutes with the echo routers - Router is defined within openapi.go
func RegisterRoutes(router *Router) error {
// add transaction middleware
transactionConfig := transaction.Client{
EntDBClient: router.Handler.DBClient,
Logger: router.Handler.Logger,
}
mw = append(mw, transactionConfig.Middleware)
// Middleware for restricted endpoints
restrictedEndpointsMW = append(restrictedEndpointsMW, mw...)
restrictedEndpointsMW = append(restrictedEndpointsMW, ratelimit.RateLimiterWithConfig(restrictedRateLimit)) // add restricted ratelimit middleware
// Middleware for authenticated endpoints
authMW = append(authMW, mw...)
authMW = append(authMW, router.Handler.AuthMiddleware...)
// routeHandlers that take the router and handler as input
routeHandlers := []interface{}{
registerReadinessHandler,
registerLivenessHandler,
registerMetricsHandler,
}
for _, route := range routeHandlers {
if err := route.(func(*Router) error)(router); err != nil {
return err
}
}
return nil
}