1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-25 23:24:22 +00:00
fiber/group.go

139 lines
4.0 KiB
Go
Raw Normal View History

2020-02-22 17:03:30 -05:00
// 🚀 Fiber is an Express inspired web framework written in Go with 💖
// 📌 API Documentation: https://fiber.wiki
// 📝 Github Repository: https://github.com/gofiber/fiber
2020-02-21 18:07:43 +01:00
package fiber
2020-02-26 19:31:43 -05:00
import (
2020-02-27 04:10:26 -05:00
"log"
2020-02-26 19:31:43 -05:00
"net/http"
2020-02-27 04:10:26 -05:00
"reflect"
2020-02-26 19:31:43 -05:00
)
2020-02-21 18:07:43 +01:00
// Group ...
type Group struct {
prefix string
app *App
}
2020-02-26 19:31:43 -05:00
// Group : https://fiber.wiki/application#group
2020-02-27 04:10:26 -05:00
func (grp *Group) Group(prefix string, handlers ...func(*Ctx)) *Group {
2020-03-04 12:30:29 +01:00
prefix = groupPaths(grp.prefix, prefix)
2020-02-26 19:31:43 -05:00
if len(handlers) > 0 {
2020-03-03 12:21:34 -05:00
grp.app.registerMethod("USE", prefix, handlers...)
2020-02-21 18:07:43 +01:00
}
return &Group{
2020-02-27 04:10:26 -05:00
prefix: prefix,
2020-02-21 18:07:43 +01:00
app: grp.app,
}
}
2020-02-26 19:31:43 -05:00
// Static : https://fiber.wiki/application#static
2020-03-04 12:30:29 +01:00
func (grp *Group) Static(prefix, root string) *Group {
prefix = groupPaths(grp.prefix, prefix)
grp.app.registerStatic(prefix, root)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-27 04:10:26 -05:00
// Use : https://fiber.wiki/application#http-methods
func (grp *Group) Use(args ...interface{}) *Group {
var path = ""
var handlers []func(*Ctx)
for i := 0; i < len(args); i++ {
switch arg := args[i].(type) {
case string:
path = arg
case func(*Ctx):
handlers = append(handlers, arg)
default:
2020-03-04 12:30:29 +01:00
log.Fatalf("Invalid handler: %v", reflect.TypeOf(arg))
2020-02-27 04:10:26 -05:00
}
}
2020-03-03 12:21:34 -05:00
path = groupPaths(grp.prefix, path)
grp.app.registerMethod("USE", path, handlers...)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-26 19:31:43 -05:00
// Connect : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (grp *Group) Connect(path string, handlers ...func(*Ctx)) *Group {
2020-03-03 12:21:34 -05:00
path = groupPaths(grp.prefix, path)
grp.app.registerMethod(http.MethodConnect, path, handlers...)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-26 19:31:43 -05:00
// Put : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (grp *Group) Put(path string, handlers ...func(*Ctx)) *Group {
2020-03-03 12:21:34 -05:00
path = groupPaths(grp.prefix, path)
grp.app.registerMethod(http.MethodPut, path, handlers...)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-26 19:31:43 -05:00
// Post : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (grp *Group) Post(path string, handlers ...func(*Ctx)) *Group {
2020-03-03 12:21:34 -05:00
path = groupPaths(grp.prefix, path)
grp.app.registerMethod(http.MethodPost, path, handlers...)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-26 19:31:43 -05:00
// Delete : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (grp *Group) Delete(path string, handlers ...func(*Ctx)) *Group {
2020-03-03 12:21:34 -05:00
path = groupPaths(grp.prefix, path)
grp.app.registerMethod(http.MethodDelete, path, handlers...)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-26 19:31:43 -05:00
// Head : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (grp *Group) Head(path string, handlers ...func(*Ctx)) *Group {
2020-03-03 12:21:34 -05:00
path = groupPaths(grp.prefix, path)
grp.app.registerMethod(http.MethodHead, path, handlers...)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-26 19:31:43 -05:00
// Patch : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (grp *Group) Patch(path string, handlers ...func(*Ctx)) *Group {
2020-03-03 12:21:34 -05:00
path = groupPaths(grp.prefix, path)
grp.app.registerMethod(http.MethodPatch, path, handlers...)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-26 19:31:43 -05:00
// Options : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (grp *Group) Options(path string, handlers ...func(*Ctx)) *Group {
2020-03-03 12:21:34 -05:00
path = groupPaths(grp.prefix, path)
grp.app.registerMethod(http.MethodOptions, path, handlers...)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-26 19:31:43 -05:00
// Trace : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (grp *Group) Trace(path string, handlers ...func(*Ctx)) *Group {
2020-03-03 12:21:34 -05:00
path = groupPaths(grp.prefix, path)
grp.app.registerMethod(http.MethodTrace, path, handlers...)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-26 19:31:43 -05:00
// Get : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (grp *Group) Get(path string, handlers ...func(*Ctx)) *Group {
2020-03-03 12:21:34 -05:00
path = groupPaths(grp.prefix, path)
grp.app.registerMethod(http.MethodGet, path, handlers...)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-26 19:31:43 -05:00
// All : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (grp *Group) All(path string, handlers ...func(*Ctx)) *Group {
2020-03-03 12:21:34 -05:00
path = groupPaths(grp.prefix, path)
grp.app.registerMethod("ALL", path, handlers...)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-27 04:10:26 -05:00
// WebSocket : https://fiber.wiki/application#websocket
2020-03-14 12:30:21 +01:00
func (grp *Group) WebSocket(path string, handle func(*Ctx)) *Group {
2020-03-04 12:30:29 +01:00
path = groupPaths(grp.prefix, path)
grp.app.registerWebSocket(http.MethodGet, path, handle)
2020-02-21 18:07:43 +01:00
return grp
}
2020-02-27 04:10:26 -05:00
// Recover : https://fiber.wiki/application#recover
func (grp *Group) Recover(handler func(*Ctx)) {
2020-03-04 12:30:29 +01:00
log.Println("Warning: Recover(handler) is deprecated since v1.8.2, please use middleware.Recover(handler, error) instead.")
2020-02-27 04:10:26 -05:00
grp.app.recover = handler
}