-
Notifications
You must be signed in to change notification settings - Fork 0
/
routeStructure.go
83 lines (70 loc) · 2.15 KB
/
routeStructure.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
package customRouter
import (
"github.com/go-chi/chi"
"net/http"
)
//RouteStructure -
type RouteStructure struct {
Uses []func(http.Handler) http.Handler
Methods []MethodRoutePatternInfo
Mounts []RoutePatternInfo
Routes map[string]*RouteStructure
Groups []RouteStructure
}
//MethodRoutePatternInfo -
type MethodRoutePatternInfo struct {
Method string // It's recommend to use constants of net/httpHandler/method.go
Pattern string
Handler http.Handler
}
//RoutePatternInfo -
type RoutePatternInfo struct {
Pattern string
OtherParams []interface{}
ServeFunc func(router *chi.Mux, otherParams ...interface{})
}
//PrepareRoutes -
func (routeStructure RouteStructure) PrepareRoutes(router *chi.Mux) {
router.Use(routeStructure.Uses...)
for _, method := range routeStructure.Methods {
//fmt.Println("Method:", method)
router.Method(method.Method, method.Pattern, method.Handler)
}
for _, mountInfo := range routeStructure.Mounts {
newRouter := chi.NewRouter()
mountInfo.ServeFunc(newRouter, mountInfo.OtherParams...)
router.Mount(mountInfo.Pattern, newRouter)
}
for pattern, routeInfo := range routeStructure.Routes {
router.Route(pattern, func(newRouter chi.Router) {
(*routeInfo).prepareRoutesNewRouter(newRouter)
})
}
for _, groupInfo := range routeStructure.Groups {
router.Group(func(newRouter chi.Router) {
groupInfo.prepareRoutesNewRouter(newRouter)
})
}
}
//prepareRoutesNewRouter -
func (routeStructure RouteStructure) prepareRoutesNewRouter(router chi.Router) {
router.Use(routeStructure.Uses...)
for _, method := range routeStructure.Methods {
router.Method(method.Method, method.Pattern, method.Handler)
}
for _, mountInfo := range routeStructure.Mounts {
newRouter := chi.NewRouter()
mountInfo.ServeFunc(newRouter, mountInfo.OtherParams...)
router.Mount(mountInfo.Pattern, newRouter)
}
for pattern, routeInfo := range routeStructure.Routes {
router.Route(pattern, func(newRouter chi.Router) {
(*routeInfo).prepareRoutesNewRouter(newRouter)
})
}
for _, groupInfo := range routeStructure.Groups {
router.Group(func(newRouter chi.Router) {
groupInfo.prepareRoutesNewRouter(newRouter)
})
}
}