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
|
|
|
"strings"
|
|
|
|
)
|
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-02-26 19:31:43 -05:00
|
|
|
if len(prefix) > 0 && prefix[0] != '/' && prefix[0] != '*' {
|
|
|
|
prefix = "/" + prefix
|
2020-02-21 18:07:43 +01:00
|
|
|
}
|
|
|
|
// When grouping, always remove single slash
|
2020-02-27 04:10:26 -05:00
|
|
|
if len(grp.prefix) > 0 && prefix == "/" {
|
2020-02-26 19:31:43 -05:00
|
|
|
prefix = ""
|
2020-02-21 18:07:43 +01:00
|
|
|
}
|
|
|
|
// Prepent group prefix if exist
|
2020-02-27 04:10:26 -05:00
|
|
|
prefix = grp.prefix + prefix
|
2020-02-21 18:07:43 +01:00
|
|
|
// Clean path by removing double "//" => "/"
|
2020-02-27 04:10:26 -05:00
|
|
|
prefix = strings.Replace(prefix, "//", "/", -1)
|
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-02-21 18:07:43 +01:00
|
|
|
func (grp *Group) Static(args ...string) *Group {
|
|
|
|
grp.app.registerStatic(grp.prefix, args...)
|
|
|
|
return grp
|
|
|
|
}
|
|
|
|
|
2020-03-03 12:21:34 -05:00
|
|
|
func groupPaths(prefix, path string) string {
|
|
|
|
// `/v1`+`/` => `/v1`+``
|
|
|
|
if path == "/" {
|
|
|
|
path = prefix
|
|
|
|
} else {
|
|
|
|
path = prefix + path
|
|
|
|
}
|
|
|
|
// Remove duplicate slashes `//`
|
|
|
|
path = strings.Replace(path, "//", "/", -1)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
log.Fatalf("Invalid handlerrrr: %v", reflect.TypeOf(arg))
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
func (grp *Group) WebSocket(path string, handler func(*Conn)) *Group {
|
|
|
|
grp.app.registerWebSocket(http.MethodGet, grp.prefix, path, handler)
|
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)) {
|
|
|
|
grp.app.recover = handler
|
|
|
|
}
|