Skip to content

Commit bc9404e

Browse files
committed
feat: added NewGroup to RouterWrapper and its implementations
1 parent 961a10a commit bc9404e

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

http/route/router.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ type (
1212
HandleFunc(path string, handler http.HandlerFunc)
1313
RegisterRoute(path string, handler http.HandlerFunc)
1414
RegisterHandler(path string, handler http.Handler)
15-
RegisterRouteGroup(path string, router *Router)
15+
NewGroup(path string) (*Router, error)
16+
RegisterGroup(path string, router *Router)
1617
}
1718

1819
// Router is the route group struct
@@ -39,29 +40,34 @@ func NewRouter(path string, mode *goflagsmode.Flag, logger *Logger) *Router {
3940
}
4041
}
4142

42-
// NewRouterGroup creates a new route group
43-
func NewRouterGroup(baseRoute *Router, path string) (*Router, error) {
44-
// Check if the base route is nil
45-
if baseRoute == nil {
43+
// NewBaseRouter creates a new base router
44+
func NewBaseRouter(mode *goflagsmode.Flag, logger *Logger) *Router {
45+
return NewRouter("", mode, logger)
46+
}
47+
48+
// NewGroup creates a new router group
49+
func NewGroup(baseRouter *Router, path string) (*Router, error) {
50+
// Check if the base router is nil
51+
if baseRouter == nil {
4652
return nil, ErrNilRouter
4753
}
4854

49-
// Check the base route path
55+
// Check the base router path
5056
routerPath := path
51-
if baseRoute.path != "/" {
52-
routerPath = baseRoute.path + path
57+
if baseRouter.path != "/" {
58+
routerPath = baseRouter.path + path
5359
}
5460

5561
// Create a new router
5662
instance := &Router{
5763
mux: http.NewServeMux(),
58-
logger: baseRoute.logger,
64+
logger: baseRouter.logger,
5965
path: routerPath,
60-
mode: baseRoute.mode,
66+
mode: baseRouter.mode,
6167
}
6268

63-
// Register the route group
64-
baseRoute.RegisterRouteGroup(path, instance)
69+
// Register the group
70+
baseRouter.RegisterGroup(path, instance)
6571

6672
return instance, nil
6773
}
@@ -101,7 +107,12 @@ func (r *Router) RegisterHandler(path string, handler http.Handler) {
101107
}
102108
}
103109

104-
// RegisterRouteGroup registers a new route group with a path and a router
105-
func (r *Router) RegisterRouteGroup(path string, router *Router) {
110+
// RegisterGroup registers a new router group with a path and a router
111+
func (r *Router) RegisterGroup(path string, router *Router) {
106112
r.RegisterHandler(path, router.mux)
107113
}
114+
115+
// NewGroup creates a new router group with a path
116+
func (r *Router) NewGroup(path string) (*Router, error) {
117+
return NewGroup(r, path)
118+
}

0 commit comments

Comments
 (0)