| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
- // 🤖 Github Repository: https://github.com/gofiber/fiber
- // 📌 API Documentation: https://docs.gofiber.io
- package fiber
- import (
- "fmt"
- "reflect"
- )
- // Group struct
- type Group struct {
- app *App
- prefix string
- }
- // Use registers a middleware route.
- // Middleware matches requests beginning with the provided prefix.
- // Providing a prefix is optional, it defaults to "/".
- //
- // - group.Use(handler)
- // - group.Use("/api", handler)
- // - group.Use("/api", handler, handler)
- func (grp *Group) Use(args ...interface{}) Router {
- var path = ""
- var handlers []Handler
- for i := 0; i < len(args); i++ {
- switch arg := args[i].(type) {
- case string:
- path = arg
- case Handler:
- handlers = append(handlers, arg)
- default:
- panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg)))
- }
- }
- grp.app.register(methodUse, getGroupPath(grp.prefix, path), handlers...)
- return grp
- }
- // Get registers a route for GET methods that requests a representation
- // of the specified resource. Requests using GET should only retrieve data.
- func (grp *Group) Get(path string, handlers ...Handler) Router {
- route := grp.app.register(MethodGet, getGroupPath(grp.prefix, path), handlers...)
- // Add head route
- headRoute := route
- grp.app.addRoute(MethodHead, &headRoute)
- return grp
- }
- // Head registers a route for HEAD methods that asks for a response identical
- // to that of a GET request, but without the response body.
- func (grp *Group) Head(path string, handlers ...Handler) Router {
- return grp.Add(MethodHead, path, handlers...)
- }
- // Post registers a route for POST methods that is used to submit an entity to the
- // specified resource, often causing a change in state or side effects on the server.
- func (grp *Group) Post(path string, handlers ...Handler) Router {
- return grp.Add(MethodPost, path, handlers...)
- }
- // Put registers a route for PUT methods that replaces all current representations
- // of the target resource with the request payload.
- func (grp *Group) Put(path string, handlers ...Handler) Router {
- return grp.Add(MethodPut, path, handlers...)
- }
- // Delete registers a route for DELETE methods that deletes the specified resource.
- func (grp *Group) Delete(path string, handlers ...Handler) Router {
- return grp.Add(MethodDelete, path, handlers...)
- }
- // Connect registers a route for CONNECT methods that establishes a tunnel to the
- // server identified by the target resource.
- func (grp *Group) Connect(path string, handlers ...Handler) Router {
- return grp.Add(MethodConnect, path, handlers...)
- }
- // Options registers a route for OPTIONS methods that is used to describe the
- // communication options for the target resource.
- func (grp *Group) Options(path string, handlers ...Handler) Router {
- return grp.Add(MethodOptions, path, handlers...)
- }
- // Trace registers a route for TRACE methods that performs a message loop-back
- // test along the path to the target resource.
- func (grp *Group) Trace(path string, handlers ...Handler) Router {
- return grp.Add(MethodTrace, path, handlers...)
- }
- // Patch registers a route for PATCH methods that is used to apply partial
- // modifications to a resource.
- func (grp *Group) Patch(path string, handlers ...Handler) Router {
- return grp.Add(MethodPatch, path, handlers...)
- }
- // Add ...
- func (grp *Group) Add(method, path string, handlers ...Handler) Router {
- grp.app.register(method, getGroupPath(grp.prefix, path), handlers...)
- return grp
- }
- // Static ...
- func (grp *Group) Static(prefix, root string, config ...Static) Router {
- grp.app.registerStatic(getGroupPath(grp.prefix, prefix), root, config...)
- return grp
- }
- // All ...
- func (grp *Group) All(path string, handlers ...Handler) Router {
- for _, method := range intMethod {
- grp.Add(method, path, handlers...)
- }
- return grp
- }
- // Group is used for Routes with common prefix to define a new sub-router with optional middleware.
- func (grp *Group) Group(prefix string, handlers ...Handler) Router {
- prefix = getGroupPath(grp.prefix, prefix)
- if len(handlers) > 0 {
- grp.app.register(methodUse, prefix, handlers...)
- }
- return grp.app.Group(prefix)
- }
|