-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup.go
More file actions
63 lines (52 loc) · 1.79 KB
/
group.go
File metadata and controls
63 lines (52 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package fiberoapi
import (
"github.com/gofiber/fiber/v2"
)
// OApiGroup wraps a fiber.Router and adds OpenAPI methods
type OApiGroup struct {
fiber.Router // Embedded fiber.Router (includes all standard Fiber methods)
oapi *OApiApp // Reference to the parent OApiApp
prefix string // Group prefix for path construction
}
// Implement OApiRouter interface for OApiGroup
func (g *OApiGroup) GetApp() *OApiApp {
return g.oapi
}
func (g *OApiGroup) GetPrefix() string {
return g.prefix
}
// Use adds middleware to the OApiGroup
func (g *OApiGroup) Use(middleware fiber.Handler) {
g.Router.Use(middleware)
}
// Group creates a new OApiGroup that wraps a fiber.Router
func (app *OApiApp) Group(prefix string, handlers ...fiber.Handler) *OApiGroup {
// Create the actual fiber group
fiberGroup := app.f.Group(prefix, handlers...)
return &OApiGroup{
Router: fiberGroup, // Embed the fiber.Router
oapi: app, // Keep reference to parent app
prefix: prefix, // Store prefix for path construction
}
}
// Group creates a new sub-group within this group
func (g *OApiGroup) Group(prefix string, handlers ...fiber.Handler) *OApiGroup {
// Create full prefix by combining current prefix with new prefix
fullPrefix := g.prefix + prefix
// Create the fiber group from the parent app
fiberGroup := g.oapi.f.Group(fullPrefix, handlers...)
return &OApiGroup{
Router: fiberGroup,
oapi: g.oapi,
prefix: fullPrefix,
}
}
// Group creates a new group from an OApiRouter (app or group)
func Group(router OApiRouter, prefix string, handlers ...fiber.Handler) *OApiGroup {
if app, ok := router.(*OApiApp); ok {
return app.Group(prefix, handlers...)
} else if group, ok := router.(*OApiGroup); ok {
return group.Group(prefix, handlers...)
}
panic("unsupported router type")
}