-
Notifications
You must be signed in to change notification settings - Fork 1
/
conf.go
53 lines (48 loc) · 1.24 KB
/
conf.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
package meta_gin
import (
"github.com/gin-gonic/gin"
)
type SetupConfig[M Model] struct {
Router *gin.Engine
Config *Config
Version string
GroupName string
Middlewares []gin.HandlerFunc
Decorators []Decorator
routeConfig *RouteConfig
}
type GenericConfig[M Model] struct {
SetupConfig[M]
Handlers []Handler[M]
}
func SetupGenericRouteForModel[M Model](
setupConfig GenericConfig[M],
) {
routes := []RouteInfo{}
routeHandler := &RouteHandler[M]{}
for _, handler := range setupConfig.Handlers {
routeHandler = NewRouteHandler[M](handler, setupConfig.Router)
for p, h := range handler.Handlers() {
routes = append(routes, RouteInfo{
Path: p,
Method: handler.Method(),
Handler: AddDecorators(h, setupConfig.Decorators),
Middlewares: setupConfig.Middlewares,
})
}
}
if setupConfig.routeConfig == nil {
setupConfig.routeConfig = &RouteConfig{
Version: setupConfig.Version,
GroupName: setupConfig.GroupName,
Routes: routes,
}
}
RegisterRoutes(routeHandler, *setupConfig.routeConfig)
}
func AddDecorators(handler gin.HandlerFunc, decorators []Decorator) gin.HandlerFunc {
for _, decorator := range decorators {
handler = decorator.Decorate(handler)
}
return handler
}