mirror of https://github.com/gofiber/fiber.git
Add Handler type (#410)
* Add new supporter * Add new test condition * Add Handler Typepull/404/head^2
parent
73a29062ae
commit
e8b8fdfb35
|
@ -532,9 +532,10 @@ Fiber is an open source project that runs on donations to pay the bills e.g. our
|
||||||
|
|
||||||
| | User | Donation |
|
| | User | Donation |
|
||||||
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
||||||
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
||||||
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
||||||
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
||||||
|
|  | [@ankush](https://github.com/ankush) | ☕ x 3 |
|
||||||
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
||||||
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
||||||
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
||||||
|
|
|
@ -528,9 +528,10 @@ Fiber is an open source project that runs on donations to pay the bills e.g. our
|
||||||
|
|
||||||
| | User | Donation |
|
| | User | Donation |
|
||||||
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
||||||
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
||||||
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
||||||
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
||||||
|
|  | [@ankush](https://github.com/ankush) | ☕ x 3 |
|
||||||
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
||||||
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
||||||
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
||||||
|
|
|
@ -528,9 +528,10 @@ Fiber es un proyecto open source que se mantiene a través de donaciones para pa
|
||||||
|
|
||||||
| | User | Donation |
|
| | User | Donation |
|
||||||
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
||||||
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
||||||
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
||||||
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
||||||
|
|  | [@ankush](https://github.com/ankush) | ☕ x 3 |
|
||||||
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
||||||
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
||||||
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
||||||
|
|
|
@ -528,9 +528,10 @@ Fiber is an open source project that runs on donations to pay the bills e.g. our
|
||||||
|
|
||||||
| | User | Donation |
|
| | User | Donation |
|
||||||
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
||||||
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
||||||
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
||||||
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
||||||
|
|  | [@ankush](https://github.com/ankush) | ☕ x 3 |
|
||||||
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
||||||
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
||||||
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
||||||
|
|
|
@ -530,9 +530,10 @@ Fiber is an open source project that runs on donations to pay the bills e.g. our
|
||||||
|
|
||||||
| | User | Donation |
|
| | User | Donation |
|
||||||
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
||||||
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
||||||
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
||||||
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
||||||
|
|  | [@ankush](https://github.com/ankush) | ☕ x 3 |
|
||||||
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
||||||
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
||||||
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
||||||
|
|
|
@ -532,9 +532,10 @@ Fiber is an open source project that runs on donations to pay the bills e.g. our
|
||||||
|
|
||||||
| | User | Donation |
|
| | User | Donation |
|
||||||
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
||||||
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
||||||
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
||||||
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
||||||
|
|  | [@ankush](https://github.com/ankush) | ☕ x 3 |
|
||||||
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
||||||
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
||||||
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
||||||
|
|
|
@ -532,9 +532,10 @@ Fiber is an open source project that runs on donations to pay the bills e.g. our
|
||||||
|
|
||||||
| | User | Donation |
|
| | User | Donation |
|
||||||
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
||||||
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
||||||
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
||||||
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
||||||
|
|  | [@ankush](https://github.com/ankush) | ☕ x 3 |
|
||||||
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
||||||
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
||||||
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
||||||
|
|
|
@ -532,9 +532,10 @@ Fiber is an open source project that runs on donations to pay the bills e.g. our
|
||||||
|
|
||||||
| | User | Donation |
|
| | User | Donation |
|
||||||
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
||||||
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
||||||
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
||||||
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
||||||
|
|  | [@ankush](https://github.com/ankush) | ☕ x 3 |
|
||||||
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
||||||
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
||||||
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
||||||
|
|
|
@ -528,9 +528,10 @@ Fiber is an open source project that runs on donations to pay the bills e.g. our
|
||||||
|
|
||||||
| | User | Donation |
|
| | User | Donation |
|
||||||
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
||||||
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
||||||
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
||||||
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
||||||
|
|  | [@ankush](https://github.com/ankush) | ☕ x 3 |
|
||||||
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
||||||
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
||||||
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
||||||
|
|
|
@ -526,9 +526,10 @@ Fiber, alan adı, gitbook, netlify, serverless yer sağlayıcısı giderleri ve
|
||||||
|
|
||||||
| | User | Donation |
|
| | User | Donation |
|
||||||
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
||||||
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
||||||
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
||||||
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
||||||
|
|  | [@ankush](https://github.com/ankush) | ☕ x 3 |
|
||||||
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
||||||
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
||||||
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
||||||
|
|
|
@ -531,9 +531,10 @@ Fiber is an open source project that runs on donations to pay the bills e.g. our
|
||||||
|
|
||||||
| | User | Donation |
|
| | User | Donation |
|
||||||
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
| :---------------------------------------------------------- | :---------------------------------------------- | :------- |
|
||||||
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
|  | [@thomasvvugt](https://github.com/thomasvvugt) | ☕ x 5 |
|
||||||
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
|  | [@ekaputra07](https://github.com/ekaputra07) | ☕ x 5 |
|
||||||
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
|  | [@candidosales](https://github.com/candidosales)| ☕ x 5 |
|
||||||
|
|  | [@ankush](https://github.com/ankush) | ☕ x 3 |
|
||||||
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
|  | [@bihe](https://github.com/bihe) | ☕ x 3 |
|
||||||
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
|  | [@justdave](https://github.com/justdave) | ☕ x 3 |
|
||||||
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
|  | [@koddr](https://github.com/koddr) | ☕ x 1 |
|
||||||
|
|
33
app.go
33
app.go
|
@ -31,6 +31,9 @@ const Version = "1.10.0"
|
||||||
// Map is a shortcut for map[string]interface{}, usefull for JSON returns
|
// Map is a shortcut for map[string]interface{}, usefull for JSON returns
|
||||||
type Map map[string]interface{}
|
type Map map[string]interface{}
|
||||||
|
|
||||||
|
// Handler ...
|
||||||
|
type Handler = func(*Ctx)
|
||||||
|
|
||||||
// App denotes the Fiber application.
|
// App denotes the Fiber application.
|
||||||
type App struct {
|
type App struct {
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
|
@ -199,68 +202,68 @@ func New(settings ...*Settings) *App {
|
||||||
// - app.Use("/api", handler, handler)
|
// - app.Use("/api", handler, handler)
|
||||||
func (app *App) Use(args ...interface{}) *Route {
|
func (app *App) Use(args ...interface{}) *Route {
|
||||||
var prefix string
|
var prefix string
|
||||||
var handlers []func(*Ctx)
|
var handlers []Handler
|
||||||
|
|
||||||
for i := 0; i < len(args); i++ {
|
for i := 0; i < len(args); i++ {
|
||||||
switch arg := args[i].(type) {
|
switch arg := args[i].(type) {
|
||||||
case string:
|
case string:
|
||||||
prefix = arg
|
prefix = arg
|
||||||
case func(*Ctx):
|
case Handler:
|
||||||
handlers = append(handlers, arg)
|
handlers = append(handlers, arg)
|
||||||
default:
|
default:
|
||||||
log.Fatalf("Use: Invalid func(c *fiber.Ctx) handler %v", reflect.TypeOf(arg))
|
log.Fatalf("Use: Invalid Handler %v", reflect.TypeOf(arg))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return app.register("USE", prefix, handlers...)
|
return app.register("USE", prefix, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get ...
|
// Get ...
|
||||||
func (app *App) Get(path string, handlers ...func(*Ctx)) *Route {
|
func (app *App) Get(path string, handlers ...Handler) *Route {
|
||||||
return app.Add(MethodGet, path, handlers...)
|
return app.Add(MethodGet, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Head ...
|
// Head ...
|
||||||
func (app *App) Head(path string, handlers ...func(*Ctx)) *Route {
|
func (app *App) Head(path string, handlers ...Handler) *Route {
|
||||||
return app.Add(MethodHead, path, handlers...)
|
return app.Add(MethodHead, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post ...
|
// Post ...
|
||||||
func (app *App) Post(path string, handlers ...func(*Ctx)) *Route {
|
func (app *App) Post(path string, handlers ...Handler) *Route {
|
||||||
return app.Add(MethodPost, path, handlers...)
|
return app.Add(MethodPost, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put ...
|
// Put ...
|
||||||
func (app *App) Put(path string, handlers ...func(*Ctx)) *Route {
|
func (app *App) Put(path string, handlers ...Handler) *Route {
|
||||||
return app.Add(MethodPut, path, handlers...)
|
return app.Add(MethodPut, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete ...
|
// Delete ...
|
||||||
func (app *App) Delete(path string, handlers ...func(*Ctx)) *Route {
|
func (app *App) Delete(path string, handlers ...Handler) *Route {
|
||||||
return app.Add(MethodDelete, path, handlers...)
|
return app.Add(MethodDelete, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect ...
|
// Connect ...
|
||||||
func (app *App) Connect(path string, handlers ...func(*Ctx)) *Route {
|
func (app *App) Connect(path string, handlers ...Handler) *Route {
|
||||||
return app.Add(MethodConnect, path, handlers...)
|
return app.Add(MethodConnect, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options ...
|
// Options ...
|
||||||
func (app *App) Options(path string, handlers ...func(*Ctx)) *Route {
|
func (app *App) Options(path string, handlers ...Handler) *Route {
|
||||||
return app.Add(MethodOptions, path, handlers...)
|
return app.Add(MethodOptions, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trace ...
|
// Trace ...
|
||||||
func (app *App) Trace(path string, handlers ...func(*Ctx)) *Route {
|
func (app *App) Trace(path string, handlers ...Handler) *Route {
|
||||||
return app.Add(MethodTrace, path, handlers...)
|
return app.Add(MethodTrace, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Patch ...
|
// Patch ...
|
||||||
func (app *App) Patch(path string, handlers ...func(*Ctx)) *Route {
|
func (app *App) Patch(path string, handlers ...Handler) *Route {
|
||||||
return app.Add(MethodPatch, path, handlers...)
|
return app.Add(MethodPatch, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add ...
|
// Add ...
|
||||||
func (app *App) Add(method, path string, handlers ...func(*Ctx)) *Route {
|
func (app *App) Add(method, path string, handlers ...Handler) *Route {
|
||||||
return app.register(method, path, handlers...)
|
return app.register(method, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,7 +273,7 @@ func (app *App) Static(prefix, root string, config ...Static) *Route {
|
||||||
}
|
}
|
||||||
|
|
||||||
// All ...
|
// All ...
|
||||||
func (app *App) All(path string, handlers ...func(*Ctx)) []*Route {
|
func (app *App) All(path string, handlers ...Handler) []*Route {
|
||||||
routes := make([]*Route, len(methodINT))
|
routes := make([]*Route, len(methodINT))
|
||||||
for method, i := range methodINT {
|
for method, i := range methodINT {
|
||||||
routes[i] = app.Add(method, path, handlers...)
|
routes[i] = app.Add(method, path, handlers...)
|
||||||
|
@ -279,7 +282,7 @@ func (app *App) All(path string, handlers ...func(*Ctx)) []*Route {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
|
// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
|
||||||
func (app *App) Group(prefix string, handlers ...func(*Ctx)) *Group {
|
func (app *App) Group(prefix string, handlers ...Handler) *Group {
|
||||||
if len(handlers) > 0 {
|
if len(handlers) > 0 {
|
||||||
app.register("USE", prefix, handlers...)
|
app.register("USE", prefix, handlers...)
|
||||||
}
|
}
|
||||||
|
|
36
group.go
36
group.go
|
@ -19,72 +19,72 @@ type Group struct {
|
||||||
// Middleware matches requests beginning with the provided prefix.
|
// Middleware matches requests beginning with the provided prefix.
|
||||||
// Providing a prefix is optional, it defaults to "/".
|
// Providing a prefix is optional, it defaults to "/".
|
||||||
//
|
//
|
||||||
// - app.Use(handler)
|
// - group.Use(handler)
|
||||||
// - app.Use("/api", handler)
|
// - group.Use("/api", handler)
|
||||||
// - app.Use("/api", handler, handler)
|
// - group.Use("/api", handler, handler)
|
||||||
func (grp *Group) Use(args ...interface{}) *Route {
|
func (grp *Group) Use(args ...interface{}) *Route {
|
||||||
var path = ""
|
var path = ""
|
||||||
var handlers []func(*Ctx)
|
var handlers []Handler
|
||||||
for i := 0; i < len(args); i++ {
|
for i := 0; i < len(args); i++ {
|
||||||
switch arg := args[i].(type) {
|
switch arg := args[i].(type) {
|
||||||
case string:
|
case string:
|
||||||
path = arg
|
path = arg
|
||||||
case func(*Ctx):
|
case Handler:
|
||||||
handlers = append(handlers, arg)
|
handlers = append(handlers, arg)
|
||||||
default:
|
default:
|
||||||
log.Fatalf("Use: Invalid func(c *fiber.Ctx) handler %v", reflect.TypeOf(arg))
|
log.Fatalf("Use: Invalid Handler %v", reflect.TypeOf(arg))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return grp.app.register("USE", getGroupPath(grp.prefix, path), handlers...)
|
return grp.app.register("USE", getGroupPath(grp.prefix, path), handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get ...
|
// Get ...
|
||||||
func (grp *Group) Get(path string, handlers ...func(*Ctx)) *Route {
|
func (grp *Group) Get(path string, handlers ...Handler) *Route {
|
||||||
return grp.Add(MethodGet, path, handlers...)
|
return grp.Add(MethodGet, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Head ...
|
// Head ...
|
||||||
func (grp *Group) Head(path string, handlers ...func(*Ctx)) *Route {
|
func (grp *Group) Head(path string, handlers ...Handler) *Route {
|
||||||
return grp.Add(MethodHead, path, handlers...)
|
return grp.Add(MethodHead, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post ...
|
// Post ...
|
||||||
func (grp *Group) Post(path string, handlers ...func(*Ctx)) *Route {
|
func (grp *Group) Post(path string, handlers ...Handler) *Route {
|
||||||
return grp.Add(MethodPost, path, handlers...)
|
return grp.Add(MethodPost, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put ...
|
// Put ...
|
||||||
func (grp *Group) Put(path string, handlers ...func(*Ctx)) *Route {
|
func (grp *Group) Put(path string, handlers ...Handler) *Route {
|
||||||
return grp.Add(MethodPut, path, handlers...)
|
return grp.Add(MethodPut, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete ...
|
// Delete ...
|
||||||
func (grp *Group) Delete(path string, handlers ...func(*Ctx)) *Route {
|
func (grp *Group) Delete(path string, handlers ...Handler) *Route {
|
||||||
return grp.Add(MethodDelete, path, handlers...)
|
return grp.Add(MethodDelete, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect ...
|
// Connect ...
|
||||||
func (grp *Group) Connect(path string, handlers ...func(*Ctx)) *Route {
|
func (grp *Group) Connect(path string, handlers ...Handler) *Route {
|
||||||
return grp.Add(MethodConnect, path, handlers...)
|
return grp.Add(MethodConnect, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options ...
|
// Options ...
|
||||||
func (grp *Group) Options(path string, handlers ...func(*Ctx)) *Route {
|
func (grp *Group) Options(path string, handlers ...Handler) *Route {
|
||||||
return grp.Add(MethodOptions, path, handlers...)
|
return grp.Add(MethodOptions, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trace ...
|
// Trace ...
|
||||||
func (grp *Group) Trace(path string, handlers ...func(*Ctx)) *Route {
|
func (grp *Group) Trace(path string, handlers ...Handler) *Route {
|
||||||
return grp.Add(MethodTrace, path, handlers...)
|
return grp.Add(MethodTrace, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Patch ...
|
// Patch ...
|
||||||
func (grp *Group) Patch(path string, handlers ...func(*Ctx)) *Route {
|
func (grp *Group) Patch(path string, handlers ...Handler) *Route {
|
||||||
return grp.Add(MethodPatch, path, handlers...)
|
return grp.Add(MethodPatch, path, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add ...
|
// Add ...
|
||||||
func (grp *Group) Add(method, path string, handlers ...func(*Ctx)) *Route {
|
func (grp *Group) Add(method, path string, handlers ...Handler) *Route {
|
||||||
return grp.app.register(method, getGroupPath(grp.prefix, path), handlers...)
|
return grp.app.register(method, getGroupPath(grp.prefix, path), handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ func (grp *Group) Static(prefix, root string, config ...Static) *Route {
|
||||||
}
|
}
|
||||||
|
|
||||||
// All ...
|
// All ...
|
||||||
func (grp *Group) All(path string, handlers ...func(*Ctx)) []*Route {
|
func (grp *Group) All(path string, handlers ...Handler) []*Route {
|
||||||
routes := make([]*Route, len(methodINT))
|
routes := make([]*Route, len(methodINT))
|
||||||
for method, i := range methodINT {
|
for method, i := range methodINT {
|
||||||
routes[i] = grp.Add(method, path, handlers...)
|
routes[i] = grp.Add(method, path, handlers...)
|
||||||
|
@ -103,7 +103,7 @@ func (grp *Group) All(path string, handlers ...func(*Ctx)) []*Route {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
|
// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
|
||||||
func (grp *Group) Group(prefix string, handlers ...func(*Ctx)) *Group {
|
func (grp *Group) Group(prefix string, handlers ...Handler) *Group {
|
||||||
prefix = getGroupPath(grp.prefix, prefix)
|
prefix = getGroupPath(grp.prefix, prefix)
|
||||||
if len(handlers) > 0 {
|
if len(handlers) > 0 {
|
||||||
grp.app.register("USE", prefix, handlers...)
|
grp.app.register("USE", prefix, handlers...)
|
||||||
|
|
|
@ -24,9 +24,9 @@ type Route struct {
|
||||||
routeParams []string // Case sensitive param keys
|
routeParams []string // Case sensitive param keys
|
||||||
|
|
||||||
// Public fields
|
// Public fields
|
||||||
Path string // Original registered route path
|
Path string // Original registered route path
|
||||||
Method string // HTTP method
|
Method string // HTTP method
|
||||||
Handlers []func(*Ctx) // Ctx handlers
|
Handlers []Handler // Ctx handlers
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) match(path, original string) (match bool, values []string) {
|
func (r *Route) match(path, original string) (match bool, values []string) {
|
||||||
|
@ -116,7 +116,7 @@ func (app *App) handler(rctx *fasthttp.RequestCtx) {
|
||||||
app.ReleaseCtx(ctx)
|
app.ReleaseCtx(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) register(method, pathRaw string, handlers ...func(*Ctx)) *Route {
|
func (app *App) register(method, pathRaw string, handlers ...Handler) *Route {
|
||||||
// Uppercase HTTP methods
|
// Uppercase HTTP methods
|
||||||
method = utils.ToUpper(method)
|
method = utils.ToUpper(method)
|
||||||
// Check if the HTTP method is valid unless it's USE
|
// Check if the HTTP method is valid unless it's USE
|
||||||
|
|
|
@ -61,6 +61,9 @@ func Test_Utils_getGroupPath(t *testing.T) {
|
||||||
res := getGroupPath("/v1", "/")
|
res := getGroupPath("/v1", "/")
|
||||||
utils.AssertEqual(t, "/v1", res)
|
utils.AssertEqual(t, "/v1", res)
|
||||||
|
|
||||||
|
res = getGroupPath("/v1/", "/")
|
||||||
|
utils.AssertEqual(t, "/v1/", res)
|
||||||
|
|
||||||
res = getGroupPath("/v1", "/")
|
res = getGroupPath("/v1", "/")
|
||||||
utils.AssertEqual(t, "/v1", res)
|
utils.AssertEqual(t, "/v1", res)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue