2022-09-18 17:35:57 +08:00
|
|
|
// โก๏ธ Fiber is an Express inspired web framework written in Go with โ๏ธ
|
2025-10-22 09:47:12 -04:00
|
|
|
// ๐ค GitHub Repository: https://github.com/gofiber/fiber
|
2022-09-18 17:35:57 +08:00
|
|
|
// ๐ API Documentation: https://docs.gofiber.io
|
|
|
|
|
|
|
|
|
|
package fiber
|
|
|
|
|
|
2025-09-28 08:28:09 +02:00
|
|
|
// Register defines all router handle interface generate by RouteChain().
|
2022-09-18 17:35:57 +08:00
|
|
|
type Register interface {
|
2025-10-15 02:12:29 -04:00
|
|
|
All(handler any, handlers ...any) Register
|
|
|
|
|
Get(handler any, handlers ...any) Register
|
|
|
|
|
Head(handler any, handlers ...any) Register
|
|
|
|
|
Post(handler any, handlers ...any) Register
|
|
|
|
|
Put(handler any, handlers ...any) Register
|
|
|
|
|
Delete(handler any, handlers ...any) Register
|
|
|
|
|
Connect(handler any, handlers ...any) Register
|
|
|
|
|
Options(handler any, handlers ...any) Register
|
|
|
|
|
Trace(handler any, handlers ...any) Register
|
|
|
|
|
Patch(handler any, handlers ...any) Register
|
|
|
|
|
|
|
|
|
|
Add(methods []string, handler any, handlers ...any) Register
|
2022-09-18 17:35:57 +08:00
|
|
|
|
2025-09-28 08:28:09 +02:00
|
|
|
RouteChain(path string) Register
|
2022-09-18 17:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-03 09:23:01 +01:00
|
|
|
var _ Register = (*Registering)(nil)
|
2022-09-18 17:35:57 +08:00
|
|
|
|
2025-09-19 09:01:52 -04:00
|
|
|
// Registering provides route registration helpers for a specific path on the
|
|
|
|
|
// application instance.
|
2022-09-18 17:35:57 +08:00
|
|
|
type Registering struct {
|
2025-09-28 08:28:09 +02:00
|
|
|
app *App
|
|
|
|
|
group *Group
|
2022-09-18 17:35:57 +08:00
|
|
|
|
|
|
|
|
path string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All registers a middleware route that will match requests
|
|
|
|
|
// with the provided path which is stored in register struct.
|
|
|
|
|
//
|
2025-09-28 08:28:09 +02:00
|
|
|
// app.RouteChain("/").All(func(c fiber.Ctx) error {
|
2022-09-18 17:35:57 +08:00
|
|
|
// return c.Next()
|
|
|
|
|
// })
|
2025-09-28 08:28:09 +02:00
|
|
|
// app.RouteChain("/api").All(func(c fiber.Ctx) error {
|
2022-09-18 17:35:57 +08:00
|
|
|
// return c.Next()
|
|
|
|
|
// })
|
2025-09-28 08:28:09 +02:00
|
|
|
// app.RouteChain("/api").All(handler, func(c fiber.Ctx) error {
|
2022-09-18 17:35:57 +08:00
|
|
|
// return c.Next()
|
|
|
|
|
// })
|
|
|
|
|
//
|
|
|
|
|
// This method will match all HTTP verbs: GET, POST, PUT, HEAD etc...
|
2025-10-15 02:12:29 -04:00
|
|
|
func (r *Registering) All(handler any, handlers ...any) Register {
|
|
|
|
|
converted := collectHandlers("register", append([]any{handler}, handlers...)...)
|
|
|
|
|
r.app.register([]string{methodUse}, r.path, r.group, converted...)
|
2022-09-18 17:35:57 +08:00
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get registers a route for GET methods that requests a representation
|
|
|
|
|
// of the specified resource. Requests using GET should only retrieve data.
|
2025-10-15 02:12:29 -04:00
|
|
|
func (r *Registering) Get(handler any, handlers ...any) Register {
|
2025-09-28 08:28:09 +02:00
|
|
|
return r.Add([]string{MethodGet}, handler, handlers...)
|
2022-09-18 17:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Head registers a route for HEAD methods that asks for a response identical
|
|
|
|
|
// to that of a GET request, but without the response body.
|
2025-10-15 02:12:29 -04:00
|
|
|
func (r *Registering) Head(handler any, handlers ...any) Register {
|
2025-02-24 08:14:19 +01:00
|
|
|
return r.Add([]string{MethodHead}, handler, handlers...)
|
2022-09-18 17:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
2025-10-15 02:12:29 -04:00
|
|
|
func (r *Registering) Post(handler any, handlers ...any) Register {
|
2025-02-24 08:14:19 +01:00
|
|
|
return r.Add([]string{MethodPost}, handler, handlers...)
|
2022-09-18 17:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Put registers a route for PUT methods that replaces all current representations
|
|
|
|
|
// of the target resource with the request payload.
|
2025-10-15 02:12:29 -04:00
|
|
|
func (r *Registering) Put(handler any, handlers ...any) Register {
|
2025-02-24 08:14:19 +01:00
|
|
|
return r.Add([]string{MethodPut}, handler, handlers...)
|
2022-09-18 17:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete registers a route for DELETE methods that deletes the specified resource.
|
2025-10-15 02:12:29 -04:00
|
|
|
func (r *Registering) Delete(handler any, handlers ...any) Register {
|
2025-02-24 08:14:19 +01:00
|
|
|
return r.Add([]string{MethodDelete}, handler, handlers...)
|
2022-09-18 17:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Connect registers a route for CONNECT methods that establishes a tunnel to the
|
|
|
|
|
// server identified by the target resource.
|
2025-10-15 02:12:29 -04:00
|
|
|
func (r *Registering) Connect(handler any, handlers ...any) Register {
|
2025-02-24 08:14:19 +01:00
|
|
|
return r.Add([]string{MethodConnect}, handler, handlers...)
|
2022-09-18 17:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Options registers a route for OPTIONS methods that is used to describe the
|
|
|
|
|
// communication options for the target resource.
|
2025-10-15 02:12:29 -04:00
|
|
|
func (r *Registering) Options(handler any, handlers ...any) Register {
|
2025-02-24 08:14:19 +01:00
|
|
|
return r.Add([]string{MethodOptions}, handler, handlers...)
|
2022-09-18 17:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Trace registers a route for TRACE methods that performs a message loop-back
|
|
|
|
|
// test along the r.Path to the target resource.
|
2025-10-15 02:12:29 -04:00
|
|
|
func (r *Registering) Trace(handler any, handlers ...any) Register {
|
2025-02-24 08:14:19 +01:00
|
|
|
return r.Add([]string{MethodTrace}, handler, handlers...)
|
2022-09-18 17:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Patch registers a route for PATCH methods that is used to apply partial
|
|
|
|
|
// modifications to a resource.
|
2025-10-15 02:12:29 -04:00
|
|
|
func (r *Registering) Patch(handler any, handlers ...any) Register {
|
2025-02-24 08:14:19 +01:00
|
|
|
return r.Add([]string{MethodPatch}, handler, handlers...)
|
2022-09-18 17:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
2022-11-01 18:45:43 +11:00
|
|
|
// Add allows you to specify multiple HTTP methods to register a route.
|
2025-11-24 14:55:40 +07:00
|
|
|
// The provided handlers are executed in order, starting with `handler` and then the variadic `handlers`.
|
2025-10-15 02:12:29 -04:00
|
|
|
func (r *Registering) Add(methods []string, handler any, handlers ...any) Register {
|
|
|
|
|
converted := collectHandlers("register", append([]any{handler}, handlers...)...)
|
|
|
|
|
r.app.register(methods, r.path, r.group, converted...)
|
2022-09-18 17:35:57 +08:00
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 08:28:09 +02:00
|
|
|
// RouteChain returns a new Register instance whose route path takes
|
2022-09-18 17:35:57 +08:00
|
|
|
// the path in the current instance as its prefix.
|
2025-09-28 08:28:09 +02:00
|
|
|
func (r *Registering) RouteChain(path string) Register {
|
2022-09-18 17:35:57 +08:00
|
|
|
// Create new group
|
2025-09-28 08:28:09 +02:00
|
|
|
route := &Registering{app: r.app, group: r.group, path: getGroupPath(r.path, path)}
|
2022-09-18 17:35:57 +08:00
|
|
|
|
|
|
|
|
return route
|
|
|
|
|
}
|