-
Notifications
You must be signed in to change notification settings - Fork 5
/
group.go
170 lines (139 loc) · 5.48 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package fiber
import (
"net/http"
"net/url"
"path/filepath"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"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 *fiber.App
originPrefix string
prefix string
originMiddlewares []httpcontract.Middleware
middlewares []httpcontract.Middleware
lastMiddlewares []httpcontract.Middleware
}
func NewGroup(config config.Config, instance *fiber.App, 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 := pathToFiberPath(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.instance.All(r.getPath(relativePath), r.getMiddlewares(handler)...)
r.clearMiddlewares()
}
func (r *Group) Get(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Get(r.getPath(relativePath), r.getMiddlewares(handler)...)
r.clearMiddlewares()
}
func (r *Group) Post(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Post(r.getPath(relativePath), r.getMiddlewares(handler)...)
r.clearMiddlewares()
}
func (r *Group) Delete(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Delete(r.getPath(relativePath), r.getMiddlewares(handler)...)
r.clearMiddlewares()
}
func (r *Group) Patch(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Patch(r.getPath(relativePath), r.getMiddlewares(handler)...)
r.clearMiddlewares()
}
func (r *Group) Put(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Put(r.getPath(relativePath), r.getMiddlewares(handler)...)
r.clearMiddlewares()
}
func (r *Group) Options(relativePath string, handler httpcontract.HandlerFunc) {
r.instance.Options(r.getPath(relativePath), r.getMiddlewares(handler)...)
r.clearMiddlewares()
}
func (r *Group) Resource(relativePath string, controller httpcontract.ResourceController) {
relativePath = r.getPath(relativePath)
r.instance.Get(relativePath, r.getMiddlewares(controller.Index)...)
r.instance.Post(relativePath, r.getMiddlewares(controller.Store)...)
r.instance.Get(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Show)...)
r.instance.Put(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Update)...)
r.instance.Patch(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Update)...)
r.instance.Delete(r.getPath(relativePath+"/{id}"), r.getMiddlewares(controller.Destroy)...)
r.clearMiddlewares()
}
func (r *Group) Static(relativePath, root string) {
relativePath = r.getPath(relativePath)
r.instance.Use(r.getMiddlewaresWithPath(r.getPath(relativePath), nil)...).Static(relativePath, root)
r.clearMiddlewares()
}
func (r *Group) StaticFile(relativePath, filePath string) {
relativePath = r.getPath(relativePath)
r.instance.Use(r.getMiddlewaresWithPath(relativePath, nil)...).Use(relativePath, func(c *fiber.Ctx) error {
dir, file := filepath.Split(filePath)
escapedFile := url.PathEscape(file)
escapedPath := filepath.Join(dir, escapedFile)
return c.SendFile(escapedPath, true)
})
r.clearMiddlewares()
}
func (r *Group) StaticFS(relativePath string, fs http.FileSystem) {
relativePath = r.getPath(relativePath)
r.instance.Use(r.getMiddlewaresWithPath(relativePath, nil)...).Use(relativePath, filesystem.New(filesystem.Config{
Root: fs,
}))
r.clearMiddlewares()
}
func (r *Group) getMiddlewares(handler httpcontract.HandlerFunc) []fiber.Handler {
var middlewares []fiber.Handler
middlewares = append(middlewares, middlewaresToFiberHandlers(r.originMiddlewares)...)
middlewares = append(middlewares, middlewaresToFiberHandlers(r.middlewares)...)
middlewares = append(middlewares, middlewaresToFiberHandlers(r.lastMiddlewares)...)
if handler != nil {
middlewares = append(middlewares, handlerToFiberHandler(handler))
}
return middlewares
}
func (r *Group) getPath(relativePath string) string {
path := pathToFiberPath(r.originPrefix + "/" + r.prefix + "/" + relativePath)
r.prefix = ""
return path
}
func (r *Group) getMiddlewaresWithPath(relativePath string, handler httpcontract.HandlerFunc) []any {
var handlers []any
handlers = append(handlers, relativePath)
middlewares := r.getMiddlewares(handler)
// Fiber will panic if no middleware is provided, So we add a dummy middleware
if len(middlewares) == 0 {
middlewares = append(middlewares, func(c *fiber.Ctx) error {
return c.Next()
})
}
for _, item := range middlewares {
handlers = append(handlers, item)
}
return handlers
}
func (r *Group) clearMiddlewares() {
r.middlewares = []httpcontract.Middleware{}
}