fiber_docs/group.go
2023-04-11 22:43:48 +02:00

94 lines
2.0 KiB
Go

package fibers
import (
"net/http"
"git.ma-al.com/goc_marek/fiber_docs/router"
"git.ma-al.com/goc_marek/fiber_docs/security"
"github.com/gofiber/fiber/v2"
)
type Group struct {
*App
Path string
Tags []string
Handlers []fiber.Handler
Securities []security.ISecurity
}
type Option func(*Group)
func Handlers(handlers ...fiber.Handler) Option {
return func(g *Group) {
for _, handler := range handlers {
g.Handlers = append(g.Handlers, handler)
}
}
}
func Tags(tags ...string) Option {
return func(g *Group) {
if g.Tags == nil {
g.Tags = tags
} else {
g.Tags = append(g.Tags, tags...)
}
}
}
func Security(securities ...security.ISecurity) Option {
return func(g *Group) {
for _, s := range securities {
g.Securities = append(g.Securities, s)
}
}
}
func (g *Group) Handle(path string, method string, r *router.Router) {
router.Handlers(g.Handlers...)(r)
router.Tags(g.Tags...)(r)
router.Security(g.Securities...)(r)
g.App.Handle(g.Path+path, method, r)
}
func (g *Group) Get(path string, router *router.Router) {
g.Handle(path, http.MethodGet, router)
}
func (g *Group) Post(path string, router *router.Router) {
g.Handle(path, http.MethodPost, router)
}
func (g *Group) Head(path string, router *router.Router) {
g.Handle(path, http.MethodHead, router)
}
func (g *Group) Patch(path string, router *router.Router) {
g.Handle(path, http.MethodPatch, router)
}
func (g *Group) Delete(path string, router *router.Router) {
g.Handle(path, http.MethodDelete, router)
}
func (g *Group) Put(path string, router *router.Router) {
g.Handle(path, http.MethodPut, router)
}
func (g *Group) Options(path string, router *router.Router) {
g.Handle(path, http.MethodOptions, router)
}
func (g *Group) Group(path string, options ...Option) *Group {
group := &Group{
App: g.App,
Path: g.Path + path,
Tags: g.Tags,
Handlers: g.Handlers,
Securities: g.Securities,
}
for _, option := range options {
option(group)
}
return group
}