forked from getevo/evo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fiber.evo.go
257 lines (219 loc) · 5.95 KB
/
fiber.evo.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package evo
import (
"github.com/gofiber/fiber/v2"
)
type Handler func(request *Request) any
type Middleware func(request *Request) error
type group struct {
app *fiber.Router
}
func (grp *group) Name(name string) *group {
var router = (*grp.app).Name(name + ".")
grp.app = &router
return grp
}
// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
func Group(path string, handlers ...Middleware) *group {
if app == nil {
panic("Access object before call Setup()")
}
var route fiber.Router
if len(handlers) > 0 {
route = app.Group(path, func(ctx *fiber.Ctx) error {
r := Upgrade(ctx)
for _, handler := range handlers {
var response = handler(r)
if response != nil {
r.WriteResponse(response)
break
}
}
return nil
})
} else {
route = app.Group(path)
}
gp := group{
app: &route,
}
return &gp
}
/*func Group(prefix string, handlers ...func(*fiber.Ctx)) *fiber.Group {
if app == nil {
panic("Access object before call Setup()")
}
return app.Group(prefix, handlers...)
}*/
// Use registers a middleware route.
// Middleware matches requests beginning with the provided prefix.
// Providing a prefix is optional, it defaults to "/"
func Use(path string, middleware Middleware) fiber.Router {
if app == nil {
panic("Access object before call Setup()")
}
var route fiber.Router
route = app.Use(path, func(ctx *fiber.Ctx) error {
r := Upgrade(ctx)
if err := middleware(r); err != nil {
return err
}
return nil
})
return route
}
// Connect : https://fiber.wiki/application#http-methods
func Connect(path string, handlers ...Handler) fiber.Router {
if app == nil {
panic("Access object before call Setup()")
}
var route fiber.Router
route = app.Connect(path, func(ctx *fiber.Ctx) error {
return handle(ctx, handlers)
})
return route
}
// Put : https://fiber.wiki/application#http-methods
func Put(path string, handlers ...Handler) fiber.Router {
if app == nil {
panic("Access object before call Setup()")
}
var route fiber.Router
route = app.Put(path, func(ctx *fiber.Ctx) error {
return handle(ctx, handlers)
})
return route
}
// Post : https://fiber.wiki/application#http-methods
func Post(path string, handlers ...Handler) fiber.Router {
if app == nil {
panic("Access object before call Setup()")
}
var route fiber.Router
route = app.Post(path, func(ctx *fiber.Ctx) error {
return handle(ctx, handlers)
})
return route
}
func handle(ctx *fiber.Ctx, handlers []Handler) error {
r := Upgrade(ctx)
for _, handler := range handlers {
var resp = handler(r)
if r._break {
return nil
}
if resp != nil {
r.WriteResponse(resp)
break
}
}
return nil
}
// Delete : https://fiber.wiki/application#http-methods
func Delete(path string, handlers ...Handler) fiber.Router {
if app == nil {
panic("Access object before call Setup()")
}
var route fiber.Router
route = app.Delete(path, func(ctx *fiber.Ctx) error {
return handle(ctx, handlers)
})
return route
}
// Head : https://fiber.wiki/application#http-methods
func Head(path string, handlers ...Handler) fiber.Router {
if app == nil {
panic("Access object before call Setup()")
}
var route fiber.Router
route = app.Head(path, func(ctx *fiber.Ctx) error {
return handle(ctx, handlers)
})
return route
}
// Patch : https://fiber.wiki/application#http-methods
func Patch(path string, handlers ...Handler) fiber.Router {
if app == nil {
panic("Access object before call Setup()")
}
var route fiber.Router
route = app.Patch(path, func(ctx *fiber.Ctx) error {
return handle(ctx, handlers)
})
return route
}
// Options : https://fiber.wiki/application#http-methods
func Options(path string, handlers ...Handler) fiber.Router {
if app == nil {
panic("Access object before call Setup()")
}
var route fiber.Router
route = app.Options(path, func(ctx *fiber.Ctx) error {
return handle(ctx, handlers)
})
return route
}
// Trace : https://fiber.wiki/application#http-methods
func Trace(path string, handlers ...Handler) fiber.Router {
if app == nil {
panic("Access object before call Setup()")
}
var route fiber.Router
route = app.Trace(path, func(ctx *fiber.Ctx) error {
return handle(ctx, handlers)
})
return route
}
// Get : https://fiber.wiki/application#http-methods
func Get(path string, handlers ...Handler) fiber.Router {
if app == nil {
panic("Access object before call Setup()")
}
var route fiber.Router
route = app.Get(path, func(ctx *fiber.Ctx) error {
return handle(ctx, handlers)
})
return route
}
// All : https://fiber.wiki/application#http-methods
func All(path string, handlers ...Handler) {
if app == nil {
panic("Access object before call Setup()")
}
app.All(path, func(ctx *fiber.Ctx) error {
return handle(ctx, handlers)
})
}
// Shutdown gracefully shuts down the server without interrupting any active connections.
// Shutdown works by first closing all open listeners and then waiting indefinitely for all connections to return to idle and then shut down.
//
// When Shutdown is called, Serve, ListenAndServe, and ListenAndServeTLS immediately return nil.
// Make sure the program doesn't exit and waits instead for Shutdown to return.
//
// Shutdown does not close keepalive connections so its recommended to set ReadTimeout to something else than 0.
func Shutdown() error {
if app == nil {
panic("Access object before call Setup()")
}
return app.Shutdown()
}
// Redirect redirects a path to another
func Redirect(path, to string, code ...int) {
if app == nil {
panic("Access object before call Setup()")
}
var redirectCode = fiber.StatusTemporaryRedirect
if len(code) > 0 {
redirectCode = code[0]
}
app.All(path, func(ctx *fiber.Ctx) error {
return ctx.Redirect(to, redirectCode)
})
}
// RedirectPermanent redirects a path to another with code 301
func RedirectPermanent(path, to string) {
Redirect(path, to, 301)
}
// RedirectTemporary redirects a path to another with code 302
func RedirectTemporary(path, to string) {
Redirect(path, to, 302)
}