-
Notifications
You must be signed in to change notification settings - Fork 3
/
group.go
139 lines (111 loc) · 5.06 KB
/
group.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
package gin
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/goravel/framework/contracts/config"
httpcontract "github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/route"
)
type Group struct {
config config.Config
instance gin.IRouter
originPrefix string
prefix string
originMiddlewares []httpcontract.Middleware
middlewares []httpcontract.Middleware
lastMiddlewares []httpcontract.Middleware
}
func NewGroup(config config.Config, instance gin.IRouter, prefix string, originMiddlewares []httpcontract.Middleware, lastMiddlewares []httpcontract.Middleware) route.Router {
return &Group{
config: config,
instance: instance,
originPrefix: prefix,
originMiddlewares: originMiddlewares,
lastMiddlewares: lastMiddlewares,
}
}
func (r *Group) Group(handler route.GroupFunc) {
var middlewares []httpcontract.Middleware
middlewares = append(middlewares, r.originMiddlewares...)
middlewares = append(middlewares, r.middlewares...)
r.middlewares = []httpcontract.Middleware{}
prefix := pathToGinPath(r.originPrefix + "/" + r.prefix)
r.prefix = ""
handler(NewGroup(r.config, r.instance, prefix, middlewares, r.lastMiddlewares))
}
func (r *Group) Prefix(addr string) route.Router {
r.prefix += "/" + addr
return r
}
func (r *Group) Middleware(middlewares ...httpcontract.Middleware) route.Router {
r.middlewares = append(r.middlewares, middlewares...)
return r
}
func (r *Group) Any(relativePath string, handler httpcontract.HandlerFunc) {
r.getRoutesWithMiddlewares().Any(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(handler)}...)
r.clearMiddlewares()
}
func (r *Group) Get(relativePath string, handler httpcontract.HandlerFunc) {
r.getRoutesWithMiddlewares().GET(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(handler)}...)
r.clearMiddlewares()
}
func (r *Group) Post(relativePath string, handler httpcontract.HandlerFunc) {
r.getRoutesWithMiddlewares().POST(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(handler)}...)
r.clearMiddlewares()
}
func (r *Group) Delete(relativePath string, handler httpcontract.HandlerFunc) {
r.getRoutesWithMiddlewares().DELETE(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(handler)}...)
r.clearMiddlewares()
}
func (r *Group) Patch(relativePath string, handler httpcontract.HandlerFunc) {
r.getRoutesWithMiddlewares().PATCH(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(handler)}...)
r.clearMiddlewares()
}
func (r *Group) Put(relativePath string, handler httpcontract.HandlerFunc) {
r.getRoutesWithMiddlewares().PUT(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(handler)}...)
r.clearMiddlewares()
}
func (r *Group) Options(relativePath string, handler httpcontract.HandlerFunc) {
r.getRoutesWithMiddlewares().OPTIONS(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(handler)}...)
r.clearMiddlewares()
}
func (r *Group) Resource(relativePath string, controller httpcontract.ResourceController) {
r.getRoutesWithMiddlewares().GET(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(controller.Index)}...)
r.getRoutesWithMiddlewares().POST(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(controller.Store)}...)
r.getRoutesWithMiddlewares().GET(pathToGinPath(relativePath+"/{id}"), []gin.HandlerFunc{handlerToGinHandler(controller.Show)}...)
r.getRoutesWithMiddlewares().PUT(pathToGinPath(relativePath+"/{id}"), []gin.HandlerFunc{handlerToGinHandler(controller.Update)}...)
r.getRoutesWithMiddlewares().PATCH(pathToGinPath(relativePath+"/{id}"), []gin.HandlerFunc{handlerToGinHandler(controller.Update)}...)
r.getRoutesWithMiddlewares().DELETE(pathToGinPath(relativePath+"/{id}"), []gin.HandlerFunc{handlerToGinHandler(controller.Destroy)}...)
r.clearMiddlewares()
}
func (r *Group) Static(relativePath, root string) {
r.getRoutesWithMiddlewares().Static(pathToGinPath(relativePath), root)
r.clearMiddlewares()
}
func (r *Group) StaticFile(relativePath, filepath string) {
r.getRoutesWithMiddlewares().StaticFile(pathToGinPath(relativePath), filepath)
r.clearMiddlewares()
}
func (r *Group) StaticFS(relativePath string, fs http.FileSystem) {
r.getRoutesWithMiddlewares().StaticFS(pathToGinPath(relativePath), fs)
r.clearMiddlewares()
}
func (r *Group) getRoutesWithMiddlewares() gin.IRoutes {
prefix := pathToGinPath(r.originPrefix + "/" + r.prefix)
r.prefix = ""
ginGroup := r.instance.Group(prefix)
var middlewares []gin.HandlerFunc
ginOriginMiddlewares := middlewaresToGinHandlers(r.originMiddlewares)
ginMiddlewares := middlewaresToGinHandlers(r.middlewares)
ginLastMiddlewares := middlewaresToGinHandlers(r.lastMiddlewares)
middlewares = append(middlewares, ginOriginMiddlewares...)
middlewares = append(middlewares, ginMiddlewares...)
middlewares = append(middlewares, ginLastMiddlewares...)
if len(middlewares) > 0 {
return ginGroup.Use(middlewares...)
}
return ginGroup
}
func (r *Group) clearMiddlewares() {
r.middlewares = []httpcontract.Middleware{}
}